DEV Community

Kazi Abdur Rakib
Kazi Abdur Rakib

Posted on

AppError middleware function create


//==> src/app/errors/AppError.ts
class AppError extends Error {
  public statusCode: number;

  constructor(statusCode: number, message: string, stack = '') {
    super(message);
    this.statusCode = statusCode;

    if (stack) {
      this.stack = stack;
    } else {
      Error.captureStackTrace(this, this.constructor);
    }
  }
}

export default AppError;

Enter fullscreen mode Exit fullscreen mode
//=> src/app/modules/academicDepartment/academicDepartment.model.ts

  if (!isDepartmentExist) {
    throw new AppError(
      httpStatus.NOT_FOUND,
      'This department does not exist! ',
    );
  }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)