UNPKG

handle-http-errors

Version:

Type-safe HTTP error handling package providing error classes, standardized responses, error handler, and built-in Express middleware support.

71 lines 2.43 kB
import { ReasonPhrases, StatusCodes } from "http-status-codes"; export class HttpError extends Error { status; code; details; constructor(status, code, message, details) { super(message); this.status = status; this.code = code; this.details = details; this.name = this.constructor.name; Error.captureStackTrace(this, this.constructor); } static isHttpError(error) { return error instanceof HttpError; } } export class ValidationError extends HttpError { constructor(message = ReasonPhrases.BAD_REQUEST, details) { super(StatusCodes.BAD_REQUEST, 'VALIDATION_ERROR', message, details); } static isValidationError(error) { return error instanceof ValidationError; } } export class BadRequestError extends HttpError { constructor(message = ReasonPhrases.BAD_REQUEST, details) { super(StatusCodes.BAD_REQUEST, 'BAD_REQUEST', message, details); } static isBadRequestError(error) { return error instanceof BadRequestError; } } export class UnauthorizedError extends HttpError { constructor(message = ReasonPhrases.UNAUTHORIZED, details) { super(StatusCodes.UNAUTHORIZED, 'UNAUTHORIZED', message, details); } static isUnauthorizedError(error) { return error instanceof UnauthorizedError; } } export class ForbiddenError extends HttpError { constructor(message = ReasonPhrases.FORBIDDEN, details) { super(StatusCodes.FORBIDDEN, 'FORBIDDEN', message, details); } static isForbiddenError(error) { return error instanceof ForbiddenError; } } export class NotFoundError extends HttpError { constructor(message = ReasonPhrases.NOT_FOUND, details) { super(StatusCodes.NOT_FOUND, 'NOT_FOUND', message, details); } static isNotFoundError(error) { return error instanceof NotFoundError; } } export class InternalServerError extends HttpError { constructor(message = ReasonPhrases.INTERNAL_SERVER_ERROR, details) { super(StatusCodes.INTERNAL_SERVER_ERROR, 'INTERNAL_ERROR', message, details); } static isInternalServerError(error) { return error instanceof InternalServerError; } } export const DEFAULT_ERROR = { status: StatusCodes.INTERNAL_SERVER_ERROR, code: 'INTERNAL_ERROR', message: ReasonPhrases.INTERNAL_SERVER_ERROR }; //# sourceMappingURL=errors.js.map