postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 357 forks source link

error on a "ready" event handler raises an empty "error" event #333

Open jfromaniello opened 10 years ago

jfromaniello commented 10 years ago

This caused me some headache:

connection.on('ready', function () {
 //something goes wrong here
 throw new Error('something wrong');
}).on('error', function (err) {
 //an error event is emitted and
 //"err" is undefined here.
});

I can work a patch for this, I think it must not emit an error event in this case. Just bubble up the error.

TheDeveloper commented 10 years ago

This happens because the ready events and optional ready callback are executed in the same stack as the parser's onMethod. The parser is wrapped by a try/catch block, catching any exceptions from this stack, including the user's event listeners and readyCallback.

If you want to let your program crash, a workaround for now would be to disable reconnect in the options, and avoid listening on any 'error' events. Of course the downside is that since errors between module and user functions are not distinguished, you would lose all graceful error control.

To address the actual issue, we need to differentiate between an exception originating from a user function vs exceptions from the parser. We could invoke the user functions inside their own try/catch and ideally bind them to process.domain if existent. If not, we could re-throw the exception.