Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

How do you handle errors in Node.js, including in async code and at the process level?

Node has several error channels and you need to cover all of them.

  • Synchronous codetry/catch.
  • Promises and async/awaittry/catch around the await, or .catch() on the chain. In Express 4 an async route handler that throws is not caught by the framework, so you need a wrapper that forwards the rejection to next(err); Express 5 handles it natively.
  • Callbacks — the error-first convention, (err, result). Check err before anything else and return early.
  • EventEmitters — listen for the 'error' event. An unhandled 'error' event throws and crashes the process, which is deliberate.

At the process level: attach handlers for uncaughtException and unhandledRejection, but use them only to log and then exit cleanly — not to keep running. After an uncaught exception the process is in an unknown state, and continuing risks corrupting data.

Note: Distinguish operational errors, such as a failed network call, which you handle and retry, from programmer errors such as a TypeError, which you should let crash so a supervisor restarts the process.

All NodeJS interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as