Skip to content

Commit 347c73b

Browse files
committed
Make context errors bite
Something, I suspect promise machinery, was preventing unhandled 'error' events from killing the program. For that reason, the error handler now uses setImmediate to emit the error. Errors thrown in the ready handler were being stifled, as well, so I've rearranged it slightly so they will trigger an error event.
1 parent d065375 commit 347c73b

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

lib/sockets.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ function bufferify(chunk, encoding) {
2424

2525
function Context(url) {
2626
EventEmitter.call(this);
27-
var onError = this.emit.bind(this, 'error');
27+
var self = this;
28+
var onError = function(e) {
29+
setImmediate(self.emit.bind(self, 'error', e));
30+
}
2831
var onClose = this.emit.bind(this, 'close');
2932
var c = this._connection = amqp.connect(url);
3033
c.then(function(conn) {
3134
conn.on('error', onError);
3235
conn.on('close', onClose);
3336
});
34-
c.then(this.emit.bind(this, 'ready'),
35-
onError);
37+
c.then(this.emit.bind(this, 'ready')).then(null, onError);
3638
};
3739
inherits(Context, EventEmitter);
3840

@@ -58,7 +60,7 @@ Context.prototype.socket = function(type, options) {
5860
}), options);
5961
return s;
6062
}
61-
else throw('Undefined socket type ' + type);
63+
else throw new Error('Undefined socket type ' + type);
6264
};
6365

6466
Context.prototype.close = function(callback) {

0 commit comments

Comments
 (0)