Handle errors centrally. Not within middlewares

One Paragraph Explainer

Without one dedicated object for error handling, greater are the chances of important errors hiding under the radar due to improper handling. The error handler object is responsible for making the error visible, for example by writing to a well-formatted logger, sending events to some monitoring product like Sentry, Rollbar, or Raygun. Most web frameworks, like Express, provide an error handling middleware mechanism. A typical error handling flow might be: Some module throws an error -> API router catches the error -> it propagates the error to the middleware (e.g. Express, KOA) who is responsible for catching errors -> a centralized error handler is called -> the middleware is being told whether this error is an untrusted error (not operational) so it can restart the app gracefully. Note that it’s a common, yet wrong, practice to handle errors within Express middleware – doing so will not cover errors that are thrown in non-web interfaces.

Code Example – a typical error flow

Javascript

  1. // DAL layer, we don't handle errors here
  2. DB.addDocument(newCustomer, (error, result) => {
  3. if (error)
  4. throw new Error('Great error explanation comes here', other useful parameters)
  5. });
  6. // API route code, we catch both sync and async errors and forward to the middleware
  7. try {
  8. customerService.addNew(req.body).then((result) => {
  9. res.status(200).json(result);
  10. }).catch((error) => {
  11. next(error)
  12. });
  13. }
  14. catch (error) {
  15. next(error);
  16. }
  17. // Error handling middleware, we delegate the handling to the centralized error handler
  18. app.use(async (err, req, res, next) => {
  19. const isOperationalError = await errorHandler.handleError(err);
  20. if (!isOperationalError) {
  21. next(err);
  22. }
  23. });

Typescript

  1. // DAL layer, we don't handle errors here
  2. DB.addDocument(newCustomer, (error: Error, result: Result) => {
  3. if (error)
  4. throw new Error('Great error explanation comes here', other useful parameters)
  5. });
  6. // API route code, we catch both sync and async errors and forward to the middleware
  7. try {
  8. customerService.addNew(req.body).then((result: Result) => {
  9. res.status(200).json(result);
  10. }).catch((error: Error) => {
  11. next(error)
  12. });
  13. }
  14. catch (error) {
  15. next(error);
  16. }
  17. // Error handling middleware, we delegate the handling to the centralized error handler
  18. app.use(async (err: Error, req: Request, res: Response, next: NextFunction) => {
  19. const isOperationalError = await errorHandler.handleError(err);
  20. if (!isOperationalError) {
  21. next(err);
  22. }
  23. });

Code example – handling errors within a dedicated object

Javascript

  1. module.exports.handler = new errorHandler();
  2. function errorHandler() {
  3. this.handleError = async (err) => {
  4. await logger.logError(err);
  5. await sendMailToAdminIfCritical;
  6. await saveInOpsQueueIfCritical;
  7. await determineIfOperationalError;
  8. };
  9. }

Typescript

  1. class ErrorHandler {
  2. public async handleError(err: Error): Promise<void> {
  3. await logger.logError(err);
  4. await sendMailToAdminIfCritical();
  5. await saveInOpsQueueIfCritical();
  6. await determineIfOperationalError();
  7. };
  8. }
  9. export const handler = new ErrorHandler();

Code Example – Anti Pattern: handling errors within the middleware

Javascript

  1. // middleware handling the error directly, who will handle Cron jobs and testing errors?
  2. app.use((err, req, res, next) => {
  3. logger.logError(err);
  4. if (err.severity == errors.high) {
  5. mailer.sendMail(configuration.adminMail, 'Critical error occured', err);
  6. }
  7. if (!err.isOperational) {
  8. next(err);
  9. }
  10. });

Typescript

  1. // middleware handling the error directly, who will handle Cron jobs and testing errors?
  2. app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  3. logger.logError(err);
  4. if (err.severity == errors.high) {
  5. mailer.sendMail(configuration.adminMail, 'Critical error occured', err);
  6. }
  7. if (!err.isOperational) {
  8. next(err);
  9. }
  10. });

Blog Quote: “Sometimes lower levels can’t do anything useful except propagate the error to their caller”

From the blog Joyent, ranked 1 for the keywords “Node.js error handling”

…You may end up handling the same error at several levels of the stack. This happens when lower levels can’t do anything useful except propagate the error to their caller, which propagates the error to its caller, and so on. Often, only the top-level caller knows what the appropriate response is, whether that’s to retry the operation, report an error to the user, or something else. But that doesn’t mean you should try to report all errors to a single top-level callback, because that callback itself can’t know in what context the error occurred…

Blog Quote: “Handling each err individually would result in tremendous duplication”

From the blog JS Recipes ranked 17 for the keywords “Node.js error handling”

……In Hackathon Starter api.js controller alone, there are over 79 occurrences of error objects. Handling each err individually would result in a tremendous amount of code duplication. The next best thing you can do is to delegate all error handling logic to an Express middleware…

Blog Quote: “HTTP errors have no place in your database code”

From the blog Daily JS ranked 14 for the keywords “Node.js error handling”

……You should set useful properties in error objects, but use such properties consistently. And, don’t cross the streams: HTTP errors have no place in your database code. Or for browser developers, Ajax errors have a place in the code that talks to the server, but not code that processes Mustache templates…