Trakkasure / mikronode

Mikrotik API for Node
241 stars 97 forks source link

Channel error chaining #17

Closed lukyer closed 8 years ago

lukyer commented 8 years ago

If error occurs during 1. write(), no other write() can end with success. Both callback/promise approaches are problematic.

Example:

var chan = conn.openChannel();
    chan.write('/ip/address/printXcauseError', function() {
        chan.on('done', function(data) {
            var parsed = MikroNode.parseItems(data);
            parsed.forEach(function(item) {
                console.log('Interface/IP: ' + item.interface + "/" + item.address);
            });
        });
        chan.once('trap', function(trap, chan) {
            console.log('Command failed: ' + trap);
        });
        chan.once('error', function(err, chan) {
            console.log('Oops: ' + err);
        });
    });
    chan.write('/ip/address/print', function() {
        chan.on('done', function(data) {
            var parsed = MikroNode.parseItems(data);
            parsed.forEach(function(item) {
                console.log('Interface/IP: ' + item.interface + "/" + item.address);
            });
        });
        chan.once('trap', function(trap, chan) {
            console.log('Command failed: ' + trap);
        });
        chan.once('error', function(err, chan) {
            console.log('Oops: ' + err);
        });
    });
    chan.close();

Expected result: Command failed: no such command() Interface/IP: ether2-master-local/10.245.235.1/24 Interface/IP: internal-mikrotik/192.168.88.1/24

Obtained result: Command failed: no such command() Command failed: no such command()

lukyer commented 8 years ago

According to debug output, it is probably caused because of every command issued on the same channel sets .tag to the same value. Then error is probably returned by mikrotik because of that same tag id.

gtjoseph commented 8 years ago

Create a pull request in my repo (https://github.com/f5eng/mikronode-ng) and I'll test and merge it, then publish to npm (mikronode-ng).

Trakkasure commented 8 years ago

Thanks for your work on this code base. I have, in the past couple of months, updated to ES2015 code syntax and wrote a babel plugin to provide private class properties. In addition, I have changed the socket reader, sentence parser, connection, and channel data configuration to use RxJS streams. It does still support events.

Also, this issue has a configurable result with a new option (closeOnTrap).