recipe-backend/src/errors/httpErrors.ts
2025-11-18 20:26:28 +01:00

54 lines
1.4 KiB
TypeScript

import {HttpStatusCode} from "../apiHelpers/HttpStatusCodes.js";
/**
* Base class for all HTTP-related errors.
* Extends the built-in Error with a status code and message
* so that they can be handled in a consistent way.
*/
export class HttpError extends Error {
public statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
// Restore prototype chain (important when targeting ES5/ES2015+)
Object.setPrototypeOf(this, new.target.prototype);
}
}
export class ValidationError extends HttpError {
constructor(message: string) {
super(message, HttpStatusCode.BAD_REQUEST); // Bad Request
}
}
export class UnauthorizedError extends HttpError {
constructor(message: string = "Unauthorized") {
super(message, HttpStatusCode.UNAUTHORIZED);
}
}
export class ForbiddenError extends HttpError {
constructor(message: string = "Forbidden") {
super(message, HttpStatusCode.FORBIDDEN);
}
}
export class NotFoundError extends HttpError {
constructor(message: string = "Resource not found") {
super(message, HttpStatusCode.NOT_FOUND);
}
}
export class ConflictError extends HttpError {
constructor(message: string = "Conflict") {
super(message, 409);
}
}
export class InternalServerError extends HttpError {
constructor(message: string = "Internal Server Error") {
super(message, 500);
}
}