enmasseio / timesync

Time synchronization between peers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't make 'error' event to work

c0h1b4 opened this issue · comments

Hello,

I was making some tests with this great time sync, but when I put the:

ts.on('error', function (err) {
console.log('error on sync')
}

I turn off the server but the event is not called.

I can see in the console the following message:
timesync.js:962 POST http://localhost:3000/timesync net::ERR_CONNECTION_REFUSED

But no call to the event

From my reading in the source code, it looks like the function to emit the error:

  function emitError(err) {
    if (timesync.list('error').length > 0) {
      timesync.emit('error', err);
    }
    else {
      console.log('Error', err);
    }
  }

is reading from this timesync.list that is not being populated by the event listen subscription, so it is always 0.

If I use the ts.on('change', function()...), it works and shows the change events.

Can anyone check this?

Thanks

Thanks for reporting, I think that is a bug but have to look into this in more detail.

To properly catch errors, you can quite easily implement your own send function which neatly catches errors, you can look at the advanced examples like this one: https://github.com/enmasseio/timesync/blob/master/examples/advanced/http/index.html

I was also having trouble with error handling so the custom send method seems like the way to go. I took a quick look at the existing fetch implementation in request.brower.js and it only ever calls the error callback on a timeout so it doesn't seem to be handling connection refused or failed http calls (400 or 500 level responses) as errors. The default implementation of send also swallows errors in the catch block of its promise chain so the _handleRPCSendError will never be called causing leaks through _inProgress for any requests that do timeout.

Thanks for checking it out @azundo . It's a painful bug but I haven't had time to look into it. If you see any chance of putting together a PR with a fix, that would be very welcome!

Fixed now in v1.0.10, see #44.