Catch unhandled promise rejections

One Paragraph Explainer

Typically, most of modern Node.js/Express application code runs within promises – whether within the .then handler, a function callback or in a catch block. Surprisingly, unless a developer remembered to add a .catch clause, errors thrown at these places are not handled by the uncaughtException event-handler and disappear. Recent versions of Node added a warning message when an unhandled rejection pops, though this might help to notice when things go wrong but it’s obviously not a proper error handling method. The straightforward solution is to never forget adding .catch clauses within each promise chain call and redirect to a centralized error handler. However, building your error handling strategy only on developer’s discipline is somewhat fragile. Consequently, it’s highly recommended using a graceful fallback and subscribe to process.on('unhandledRejection', callback) – this will ensure that any promise error, if not handled locally, will get its treatment.

Code example: these errors will not get caught by any error handler (except unhandledRejection)

  1. DAL.getUserById(1).then((johnSnow) => {
  2. // this error will just vanish
  3. if(johnSnow.isAlive === false)
  4. throw new Error('ahhhh');
  5. });

Code example: Catching unresolved and rejected promises

Javascript

  1. process.on('unhandledRejection', (reason, p) => {
  2. // I just caught an unhandled promise rejection,
  3. // since we already have fallback handler for unhandled errors (see below),
  4. // let throw and let him handle that
  5. throw reason;
  6. });
  7. process.on('uncaughtException', (error) => {
  8. // I just received an error that was never handled, time to handle it and then decide whether a restart is needed
  9. errorManagement.handler.handleError(error);
  10. if (!errorManagement.handler.isTrustedError(error))
  11. process.exit(1);
  12. });

Typescript

  1. process.on('unhandledRejection', (reason: string, p: Promise<any>) => {
  2. // I just caught an unhandled promise rejection,
  3. // since we already have fallback handler for unhandled errors (see below),
  4. // let throw and let him handle that
  5. throw reason;
  6. });
  7. process.on('uncaughtException', (error: Error) => {
  8. // I just received an error that was never handled, time to handle it and then decide whether a restart is needed
  9. errorManagement.handler.handleError(error);
  10. if (!errorManagement.handler.isTrustedError(error))
  11. process.exit(1);
  12. });

Blog Quote: “If you can make a mistake, at some point you will”

From the blog James Nelson

Let’s test your understanding. Which of the following would you expect to print an error to the console?

  1. Promise.resolve('promised value').then(() => {
  2. throw new Error('error');
  3. });
  4. Promise.reject('error value').catch(() => {
  5. throw new Error('error');
  6. });
  7. new Promise((resolve, reject) => {
  8. throw new Error('error');
  9. });

I don’t know about you, but my answer is that I’d expect all of them to print an error. However, the reality is that a number of modern JavaScript environments won’t print errors for any of them.The problem with being human is that if you can make a mistake, at some point you will. Keeping this in mind, it seems obvious that we should design things in such a way that mistakes hurt as little as possible, and that means handling errors by default, not discarding them.