I'm using the WebSocket library ws for node.js and I'm trying to simulate an error event on the server that would trigger my error handling code:
ws.on('error', function(e) {
console.log("error occured");
});
I tried referencing an undefined variable in the on('message') event but that just crashed the whole server and the 'error' event never fired.
Can anyone tell me how to simulate a ws error event on the server?
Thank you!
I'm using the WebSocket library ws for node.js and I'm trying to simulate an error event on the server that would trigger my error handling code:
ws.on('error', function(e) {
console.log("error occured");
});
I tried referencing an undefined variable in the on('message') event but that just crashed the whole server and the 'error' event never fired.
Can anyone tell me how to simulate a ws error event on the server?
Thank you!
Share Improve this question asked Mar 26, 2016 at 22:43 Jared SpragueJared Sprague 2494 silver badges13 bronze badges 2-
4
Couldn't you just emit it manually (e.g.
ws.emit('error', new Error('foo'))
) or pull theerror
event handler out and call it directly? – mscdex Commented Mar 26, 2016 at 23:00 - 1 ws.emit('error') worked, thanks! – Jared Sprague Commented Mar 26, 2016 at 23:10
1 Answer
Reset to default 8Manually emitting the event should work (ws.emit('error', new Error('foo'))
) as well as calling the error
event handler directly (by pulling it out and naming it).