UNPKG

express-error-toolkit

Version:

A lightweight and developer-friendly toolkit for robust error handling in Express.js applications. Includes ready-to-use custom error classes, an async route error handler wrapper, a global error handler middleware, and a convenient 'not found' route hand

55 lines (39 loc) 2.1 kB
import { RequestHandler, Request, Response, NextFunction } from 'express'; declare class CustomAPIError extends Error { statusCode: number; errorDetails?: string | object | null; constructor(message: string, statusCode?: number, errorDetails?: string | object | null); } declare class NotFoundError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class BadRequestError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class UnauthenticatedError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class ConflictError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class ForbiddenError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class ValidationError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare class TooManyRequestsError extends CustomAPIError { constructor(message: string, errorDetails?: string | object | null); } declare function httpError(message: string, statusCode: number, errorDetails?: string | object | null): CustomAPIError; declare const asyncHandler: (fn: RequestHandler) => RequestHandler; declare const notFoundHandler: (req: Request, _res: Response, next: NextFunction) => void; declare function isCustomAPIError(err: unknown): err is CustomAPIError; interface ErrorOptions { showStack: boolean; logError: boolean; introLine: string | boolean; } declare function setErrorOptions(options: Partial<ErrorOptions>): void; declare const globalErrorHandler: (err: unknown, _req: Request, res: Response, _next: NextFunction) => void; export { BadRequestError, ConflictError, CustomAPIError, ForbiddenError, NotFoundError, TooManyRequestsError, UnauthenticatedError, ValidationError, asyncHandler, globalErrorHandler, httpError, isCustomAPIError, notFoundHandler, setErrorOptions };