Proper error handling on the Node.js ws package
Asked Answered
B

1

26

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:

Client-side

  • let server = new Websocket(path, opts)
    • Wrapping this in 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 callback
  • server.send(data, cb)
    • Wrapping this in try-catch will catch type errors, typically programmer errors
    • Adding a callback here (function (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 handled
  • server.on('error', cb)
    • Adding a callback here seems to be a good idea, as the error event is part of the EventEmitter spec, but I haven't actually caught anything with it yet
  • Heartbeat Checks (verbose, but described here)
    • This is recommended by the ws readme as a way of catching silent connection failures

Server-side

  • server.on('connection', function(connection) {...})
    • Trying 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 above
  • server.on('error', cb)
    • Seems like a good idea for the same reasons I do it on the client side above

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.

Berlauda answered 10/10, 2017 at 15:50 Comment(2)
You might look into wrapper libraries like socket.io, which could provide enhanced error handling. For situations where you can't provide communication directly to the user, you might consider an email (and) or text-message system to notify maintenance personnel when an issue happens.Miracle
Also, if you can make it work, it would probably be useful to have parallel code before closing out the old mechanism. You could run the new code in sync with the old code for a while to verify it.Miracle
A
6

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.

Androgen answered 12/12, 2019 at 9:28 Comment(1)
How to handle Uncaught Exception throw in verifyClient?Hypoplasia

© 2022 - 2024 — McMap. All rights reserved.