UNPKG

@ingeze/api-error

Version:

A TypeScript library for handling HTTP errors in Express, NestJS, and Fastify APIs.

39 lines 2.13 kB
import type { BadRequestErrorType, NotFoundErrorType, UnauthorizedErrorType, ErrorResponse, ForbiddenErrorType, ValidationErrorType } from '../types/index.js'; /** * Custom error handler class for the application. * * Extends the base `Error` class and allows defining errors with additional information * such as type, HTTP status code, message, and optional details. * * @example * throw new ErrorHandler('User not found', 404, 'NOT_FOUND') * * @property {boolean} success Indicates if the response was successful (always `false` for errors). * @property {string | NotFoundErrorType | UnauthorizedErrorType | BadRequestErrorType | ForbiddenErrorType | ValidationErrorType} type Error type. * @property {number} statusCode HTTP status code associated with the error. * @property {string} message Descriptive error message. * @property {Record<string, unknown>=} details Additional error details (optional). */ export declare class ErrorHandler extends Error { success: boolean; type: string | NotFoundErrorType | UnauthorizedErrorType | BadRequestErrorType | ForbiddenErrorType | ValidationErrorType; statusCode: number; message: string; details?: Record<string, unknown>; /** * Creates a new instance of ErrorHandler. * * @param {string} message Descriptive error message. * @param {number} [statusCode=500] HTTP status code (default is 500). * @param {string | NotFoundErrorType | UnauthorizedErrorType | BadRequestErrorType | ForbiddenErrorType | ValidationErrorType} [type='GENERIC_ERROR'] Error type. * @param {Record<string, unknown>=} details Additional error details (optional). */ constructor(message: string, statusCode?: number, type?: string | NotFoundErrorType | UnauthorizedErrorType | BadRequestErrorType | ForbiddenErrorType | ValidationErrorType, details?: Record<string, unknown>); /** * Converts the error instance to a JSON object compatible with the ErrorResponse interface. * * @returns {ErrorResponse} Serializable error object. */ toJSON(): ErrorResponse; } //# sourceMappingURL=error-handler.d.ts.map