sivy/node-statsd

exception handling

Dieterbe opened this issue · 3 comments

I would try to implement the following exception policy in node-statsd:

  • exceptions "bubble up" into the app that uses this library
  • we don't log or print to console any errors ourself, it's the toplevel app that decides how to log/write to console.
  • we document which exceptions can be raised, and where.
    I'm thinking of the following issues:
  • dns resolve (exception EADDRINFO)
  • host unreachable, connection refused (these don't give any exception, but that's ok: it's a udp socket anyway)
  • any other exception that might get raised?? I surely must be missing some, because at least require('dgram').createSocket('udp4') can raise errors, and socket.send() as well.

I'm new to node.js exception handling, so any input is welcome. For now I'm just tinkering and trying to find a workable solution, but I haven't figured it out yet. cc @msiebuhr

for now, i found sort of a way to do it:
in your main app, you can leverage the fact that you have access to c.socket and just do:

c.socket.on('error', function (exception) {
   #return console.log ("error event in socket.send(): " + exception);
   return;
});

only problem is, node prints stacktraces and I can't find how to disable it. # edit, this seems to have magically fixed itself.
related pull request #18

it may be useful to make the client itself an event emitter, this way the user doesn't need to have the knowledge of "socket" and we can emit our own events/errors if need be.

the new version adds the ability to provide a callback to each method. the callback returns an error if the message was not sent and returns the number of bytes sent if it was. This has been documented in the readme. Any other socket exceptions are bubbled up and the user can add a listener to it if they please.