Skip to content

Commit 4f99812

Browse files
committed
Merge pull request #569 from cybusio/master
Fixed error emission on receiver error
2 parents 0669cae + e642ad6 commit 4f99812

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/WebSocket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ function establishConnection(ReceiverClass, SenderClass, socket, upgradeHead) {
873873
self._receiver.onerror = function onerror(reason, errorCode) {
874874
// close the connection when the receiver reports a HyBi error code
875875
self.close(typeof errorCode !== 'undefined' ? errorCode : 1002, '');
876-
self.emit('error', reason, errorCode);
876+
self.emit('error', (reason instanceof Error) ? reason : (new Error(reason)));
877877
};
878878

879879
// finalize the client

test/WebSocket.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,40 @@ describe('WebSocket', function() {
4646
ws.should.be.an.instanceOf(WebSocket);
4747
done();
4848
});
49+
50+
it('should emit an error object when the receiver throws an error string', function(done) {
51+
52+
var wss = new WebSocketServer({port: ++port}, function() {
53+
54+
var ws = new WebSocket('ws://localhost:' + port);
55+
56+
ws.on('open', function () {
57+
ws._receiver.error('This is an error string', 1002);
58+
});
59+
60+
ws.on('error', function (error) {
61+
error.should.be.an.instanceof(Error);
62+
done();
63+
});
64+
});
65+
});
66+
67+
it('should emit an error object when the receiver throws an error object', function(done) {
68+
69+
var wss = new WebSocketServer({port: ++port}, function() {
70+
71+
var ws = new WebSocket('ws://localhost:' + port);
72+
73+
ws.on('open', function () {
74+
ws._receiver.error(new Error('This is an error object'), 1002);
75+
});
76+
77+
ws.on('error', function (error) {
78+
error.should.be.an.instanceof(Error);
79+
done();
80+
});
81+
});
82+
});
4983
});
5084

5185
describe('options', function() {

0 commit comments

Comments
 (0)