why is my express error handler not working?


I have an error handler middleware that is supposed to catch any thrown errors and display them in a serialized format.

Error handler middleware

import { NextFunction, Request, Response } from "express";
import { CustomError } from "../errors/custom-error";

export const errorHandler = (err:Error, req:Request, res:Response, next:NextFunction) => {
    if(err instanceof CustomError){
        return res.status(err.statusCode).send({errors: err.serializeErrors() })
    res.status(400).send({message: [{message:'Something went wrong'}] })


export class NotFoundError extends CustomError{
    statusCode = 404
        super('Not found error')
        Object.setPrototypeOf(this, NotFoundError.prototype)

        return [{ message: 'Not found'}]


export abstract class CustomError extends Error{
    abstract statusCode: number

    constructor(message: string){
        Object.setPrototypeOf(this, CustomError.prototype)

    abstract serializeErrors() : {
        message: string,
        field?: string


import express from 'express'
import { errorHandler } from './middleware/error-handler';
import router from './router'    

const app = express()
app.use('/api/users', router)
const PORT = 8000
app.listen(PORT, () => {
    console.log("working on port ",PORT)


import express from 'express'
import { NotFoundError } from './errors/not-found';
const router = express.Router()
router.all('/*', async (req, res) => {
    throw new NotFoundError()
export default router

Expected behaviour:

  1. An incorrect URL is supplied
  2. A NotFoundError is thrown
  3. NotFoundError being a subclass of CustomError would have 2 attributes: statusCode and serializeErrors function
  4. The middleware catches the error and sends back appropriate response

Actual behaviour:

UnhandledPromiseRejectionWarning: Error: Not found error

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function
without a catch block, or by re jecting a promise which was not
handled with .catch(). To terminate the node process on unhandled
promise rejection, use the CLI flag --unhandled-rejections=strict
(see http s://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1)

Any help or suggestion is greatly appreciated.


You don’t want to throw the error in your handler directly, because express is async code and it will just get swallowed. You need to pass that error through the next function, so that express knows what to do with it.

import express from 'express'
import { NotFoundError } from './errors/not-found';
const router = express.Router()
router.all('/*', async (req, res, next) => {
    return next(new NotFoundError());
export default router

Answered By – distrill

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published