I'm working on replacing a REST-based data pipe with a Websocket-based one, and I'm having trouble finding all the places things can go wrong. The system is production, so very bad things happen if it fails and doesn't recover. Here's what I've got so far:
let server = new Websocket(path, opts)
try-catch
will find programmer errors, like incorrect URLs, but operational errors like the server not responding correctly don't seem catchable as they're asynchronous and there's no callbackserver.send(data, cb)
try-catch
will catch type errors, typically programmer errorsfunction (err) { handleErr(err); }
) is a great catch-all on operational errors, as the callback will have a non-null err
if the send fails for any reason, so that's handledserver.on('error', cb)
error
event is part of the EventEmitter
spec, but I haven't actually caught anything with it yetws
readme as a way of catching silent connection failuresserver.on('connection', function(connection) {...})
connection.send('test', function(err) { handleErr(err); });
is a nice way of making sure the connection didn't fail somehow getting setup, before trying to use it, but it may not be necessary. Also, that should be wrapped in a try-catch
for the reasons aboveserver.on('error', cb)
It just seems like building on top of ws
with production in mind is difficult because nowhere is it documented all the different things that can go wrong, and going with a more user-friendly library like Socket.io would remove many of the performance advantages sought by using ws
. Is there anywhere documentation on all the different things that can go wrong when using ws
, or at least a guide to battle-hardening it? I feel like deploying this thing is just a gamble where any second I could get angrily called into an office to fix things.
The exception handling refers to the mechanism by which the exceptions occurring in a code while an application is running is handled. Node. js supports several mechanisms for propagating and handling errors.
An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .
You should catch all the errors in error
event.
let server = new Websocket(path, opts)
server.on('error', (error) => {
//handle error
})
Make sure that you call this right after you create the connection and before running any operation on it. Otherwise the callback will not catch any error and exception will be thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With