Skip to content

Errors

  • Errors are handled as a middleware for each route in express
export abstract class CustomError extends Error {
  abstract statusCode: number;

  constructor() {
    super(); // Invoke the constructor of the Error class
    Object.setPrototypeOf(this, CustomError.prototype); // setPrototypeOf must be called when extending a built in class
  }

  abstract serializeErrors(): { message: string; field?: string }[];
}
  • Error response structure standard example
{
  "errors": [
    {
      "message": "Database error",
      "field": "field"
    },
    {
      "message": "Validation error"
    }
  ]
}