Nội dung

In Node.js, there are various ways to handle errors. In this blog, I will show you how to handle them in a very easy way and get rid of all try-catch blocks.

Setup

Let’s assume we have a simple Node.js (Express) server running. Now install npm packages called express-async-errors and http-status-codes.

npm i express-async-errors

express-async-errors is a small package which does most of the heavy lifting. Let’s use this package in the root file, and it should always be at the top.

require('express-async-errors');

const express = require('express');

const app = express();

app.use(express.json());

const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`server is listening on port ${port}...`);
});

Custom Error Utility

Let’s create utility functions to throw different errors.

const { StatusCodes } = require("http-status-codes");

class CustomAPIError extends Error {
  constructor(message) {
    super(message);
  }
}

class BadRequestError extends CustomAPIError {
  constructor(message) {
    super(message);
    this.statusCode = StatusCodes.BAD_REQUEST;
  }
}

class ConflictError extends CustomAPIError {
  constructor(message) {
    super(message);
    this.statusCode = StatusCodes.CONFLICT;
  }
}

class NotFoundError extends CustomAPIError {
  constructor(message) {
    super(message);
    this.statusCode = StatusCodes.NOT_FOUND;
  }
}

class UnauthenticatedError extends CustomAPIError {
  constructor(message) {
    super(message);
    this.statusCode = StatusCodes.UNAUTHORIZED;
  }
}

class UnauthorizedError extends CustomAPIError {
  constructor(message) {
    super(message);
    this.statusCode = StatusCodes.FORBIDDEN;
  }
}

module.exports = {CustomAPIError, BadRequestError, ConflictError, NotFoundError, UnauthenticatedError, UnauthorizedError}

Middleware

const { StatusCodes } = require('http-status-codes');

const { CustomAPIError } = require('../errors');

const errorHandlerMiddleware = (err, req, res, next) => {
  const customError = {
    msg: 'Something went wrong, please try again',
    statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
  };

  if (err instanceof CustomAPIError) {
    customError.msg = err.message;
    customError.statusCode = err.statusCode;
  }

  res.status(customError.statusCode).json({ msg: customError.msg });
};

module.exports = errorHandlerMiddleware;

Optional Checks

You can add some optional checks too. I will show some examples for Prisma and Mongoose.

Prisma

const { StatusCodes } = require('http-status-codes');
const { Prisma } = require('@prisma/client');

const { CustomAPIError } = require('../errors');

const errorHandlerMiddleware = (err, req, res, next) => {
  const customError = {
    msg: 'Something went wrong, please try again',
    statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
  };

  if (err instanceof CustomAPIError) {
    customError.msg = err.message;
    customError.statusCode = err.statusCode;
  }

  if (err instanceof Prisma.PrismaClientKnownRequestError) {
      switch (err.code) {
        case 'P2000': {
          const key = err.meta.column_name;
          customError.msg = `${key} is too long`;
          customError.statusCode = StatusCodes.BAD_REQUEST;
          break;
        }
        case 'P2001': {
          customError.msg = 'Not found';
          customError.statusCode = StatusCodes.NOT_FOUND;
          break;
        }
        case 'P2002': {
          const key = err.meta.target[0];
          customError.msg = `Provided ${key} already exists`;
          customError.statusCode = StatusCodes.CONFLICT;
          break;
        }
        case 'P2003': {
          const key = err.meta.field_name;
          customError.msg = `${key} does not exist`;
          customError.statusCode = StatusCodes.NOT_FOUND;
          break;
        }
        case 'P2025': {
          customError.msg = 'No record found to delete';
          customError.statusCode = StatusCodes.NOT_FOUND;
          break;
        }
        default: {
          customError.msg = 'Something went wrong, please try again';
          customError.statusCode = StatusCodes.INTERNAL_SERVER_ERROR;
        }
      }
    }

  res.status(customError.statusCode).json({ msg: customError.msg });
};

module.exports = errorHandlerMiddleware;

Mongoose

const { StatusCodes } = require('http-status-codes');
const mongoose = require('mongoose');

const { CustomAPIError } = require('../errors');

const errorHandlerMiddleware = (err, req, res, next) => {
  const customError = {
    msg: 'Something went wrong, please try again',
    statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
  };

  if (err instanceof CustomAPIError) {
    customError.msg = err.message;
    customError.statusCode = err.statusCode;
  }

  if (err instanceof mongoose.Error.ValidationError) {
     if (err.name === "ValidationError") {
        customError.msg = Object.values(err.errors)
          .map((item) => item.message)
          .join(",");
        customError.statusCode = StatusCodes.BAD_REQUEST;
      }
    
      if (err.code && err.code === 11000) {
        customError.msg = `Duplicate value entered for ${Object.keys(
          err.keyValue
        )} field, please choose another value`;
        customError.statusCode = StatusCodes.BAD_REQUEST;
      }
    
      if (err.name === "CastError") {
        customError.msg = `No match found with id of ${err.value}`;
        customError.statusCode = StatusCodes.NOT_FOUND;
      }
    }

  res.status(customError.statusCode).json({ msg: customError.msg });
};

module.exports = errorHandlerMiddleware;

That’s It

Now use that middleware below all API routes. We don’t have to wrap any async code block inside try-catch. All the errors will be automatically caught by our middleware.

require('express-async-errors');

const express = require('express');

const errorHandlerMiddleware = require('./middleware/error-handler');

const app = express();

app.use(express.json());

/* api routes */
...

app.use(errorHandlerMiddleware);

const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`server is listening on port ${port}...`);
});

Hope that helps! Let me know if you have any further questions or need more clarification!

Picture of Anthony Nguyễn

Anthony Nguyễn

Xin chào, tôi là Anthony Nguyễn, một lập trình viên Full Stack với nhiều năm kinh nghiệm trong ngành công nghệ thông tin. Tôi tận dụng kiến thức và kỹ năng của mình để tạo ra các giải pháp công nghệ đột phá và đáp ứng những bài toán thách thức, phức tạp.Tôi luôn sẵn sàng để kết nối và học hỏi từ cộng đồng, cũng như chia sẻ những kiến thức và thông tin hữu ích. Với tôi, việc xây dựng kiến thức là như xây dựng một tòa nhà - cần có sự kiên nhẫn, kiến thức vững chắc, và sự cống hiến để tạo nên sự thành công.Nếu bạn có câu hỏi, ý kiến, hoặc muốn kết nối với tôi để thảo luận về công nghệ, đừng ngần ngại liên hệ tôi. Rất mong được nhận gạch đá từ các bạn!