36

After working for a couple of weeks with node.js, I found that there is a difference between node.js server errors and regular server side languages like PHP.

A simple example: IF an error happens in our website for ANY reason.

in PHP
If a user send some invalid data to server and MySQL, MySQL will output error to that specific user and the whole application won't go down.

in Nodejs
If a user send some invalid data to server and MySQL, nodejs Server will go down and so all the users will disconnect and there is no connection between users anymore.

This is a really big problem. in large web applications, It is impossible to handle all errors to avoid Nodejs server to go down, and the question is,
Is there any way to handle any unknown fatal errors and exceptions to a specific output or something like it.

3

3 Answers 3

64

You can use the uncaughtException event on the process object to do what you want, but like others have said, domains and catching/handling errors at the correct level is recommended.

process.on('uncaughtException', function(err) {
  console.log('Caught exception: ' + err);
});
3
  • 20
    Please note that if you use the uncaughtException event, you must exit the process and not continue normal operation, as the app is in an inconsistent state. nodejs.org/api/… Apr 29, 2016 at 0:06
  • @NickDaugherty or anyone. Is there any examples of how an unhandled exception would somehow cause the app to be in a inconsistent or unstable state? Just trying to understand better. Jul 31, 2020 at 21:37
  • 3
    Example: You are an HTTP server. A request comes in, you begin processing it, and an uncaught exception is thrown. You can handle the exception with the code above, but now the request is out of scope. The client is still waiting for a response, but their request was purged.... or was it? Who knows? It's undefined. What if you were halfway through updating some internal state? How do you recover from this? You don't know. You don't even know where the exception came from. The best you can do is log the exception, exit as cleanly as possible, and let the programmer figure out what happened.
    – musicin3d
    Apr 16, 2021 at 1:44
5

You should simply validate the request data within your routes, catch any error (try-catch will work here since it's a sync operation) and handle it by returning appropriate HTTP status (e.g. 400) to the caller and log the error. If you're using Express you don't even have to use try-catch as Express will catch all synchronous exceptions and allow you to handle it centrally.

I personally don't think that catching validation errors using process.on('uncaughtException') is the best match for your need for two main reasons:

  • At this location, you have no context of where the error took place and can't send back a response to the caller
  • Any type of unhandled error will arrive here, if you've no idea what type of error occured it's recommended to restart the process. It doesn't make sense to restart the process because of invalid input, for example it's too easy this way to bring the app down for any external caller (DDOS)

You may read here about other error handling best practices and specifically refer to bullets 4,6 and 10

0

You can use grackle_tracking library https://www.getgrackle.com/analytics_and_tracking

It let's you log errors, traffic to console and db and catch any errors with ability to specify a valid response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.