errors.ts 601 B

12345678910111213141516171819
  1. /**
  2. * Base HTTP error class with status code.
  3. * Throw this from route handlers to send specific HTTP errors.
  4. */
  5. export class HttpError extends Error {
  6. constructor(
  7. public statusCode: number,
  8. message: string
  9. ) {
  10. super(message);
  11. this.name = "HttpError";
  12. }
  13. }
  14. // Convenience constructors
  15. export const BadRequestError = (msg: string) => new HttpError(400, msg);
  16. export const UnauthorizedError = (msg: string) => new HttpError(401, msg);
  17. export const ForbiddenError = (msg: string) => new HttpError(403, msg);
  18. export const NotFoundError = (msg: string) => new HttpError(404, msg);