UNPKG

http-error-middleware

Version:

A simple Express middleware to handle and format error messages, improving error management in your applications.

62 lines (57 loc) 2.32 kB
import { Request, Response, NextFunction } from 'express'; declare enum SuccessfulCodes { ok = 200, created = 201, accepted = 202, NonAuthoritativeInformation = 203, noContent = 204 } declare enum ClientErrorCodes { badRequest = 400, unauthorized = 401, paymentRequired = 402, forbidden = 403, notFound = 404, methodNotAllowed = 405, notAcceptable = 406, proxyAuthenticationRequired = 407, requestTimeOut = 408, conflict = 409 } declare enum ServerErrorCodes { internalServerError = 500, notImplemented = 501, badGateway = 502, serviceUnavailable = 503, gatewayTimeout = 504 } declare class HttpError extends Error { statusCode: number; details?: object | undefined; private constructor(); static badRequest(message: string, details?: object): void; static unauthorized(message: string, details?: object): void; static paymentRequired(message: string, details?: object): void; static forbidden(message: string, details?: object): void; static notFound(message: string, details?: object): void; static methodNotAllowed(message: string, details?: object): void; static notAcceptable(message: string, details?: object): void; static proxyAuthenticationRequired(message: string, details?: object): void; static requestTimeOut(message: string, details?: object): void; static conflict(message: string, details?: object): void; static internalServerError(message: string, details?: object): void; static notImplemented(message: string, details?: object): void; static badGateway(message: string, details?: object): void; static serviceUnavailable(message: string, details?: object): void; static gatewayTimeout(message: string, details?: object): void; /** * Throw an error with status code not implemented yet in the package */ static custom(message: string, statusCode: number, details?: object): void; } interface HttpErrorMiddlewareProps { destructure: boolean; statusCodeOnResponse: boolean; } declare function httpErrorMiddleware(options?: Partial<HttpErrorMiddlewareProps>): (err: any, _req: Request, res: Response, next: NextFunction) => void; export { ClientErrorCodes, HttpError, ServerErrorCodes, SuccessfulCodes, httpErrorMiddleware };