node-js-api-response
Version:
Unified API response and error handling for Express.js in TypeScript. This package provides a middleware for consistent API responses and error handling in Express applications, making it easier to manage API responses and errors in a standardized way.
54 lines (53 loc) • 1.82 kB
TypeScript
import { ErrorKey } from '../utils/constants/error.constants';
/**
* Represents a custom API error with an HTTP status code and a boolean status.
* Extends the built-in Error class to provide additional context for API responses.
* @template T - Type of additional details included with the error.
*/
export declare class BaseError<T = any> extends Error {
/**
* The HTTP status code associated with the error.
*/
statusCode: number;
/**
* Indicates the status of the response (false for all errors).
*/
status: boolean;
/**
* Optional error code for more specific error identification.
*/
errorCode?: string;
/**
* Creates an instance of BaseError.
*
* @param statusCode - The HTTP status code for the error.
* @param message - The error message. Defaults to 'Internal server error'.
* @param errorCode - Optional error code for more specific error identification.
*/
constructor(statusCode?: number, message?: string, errorCode?: string);
}
/**
* Dynamically creates an error subclass using ErrorDefinitions
*/
export declare function BaseErrorClass(errorKey: ErrorKey): {
new (messageOverride?: string): {
/**
* The HTTP status code associated with the error.
*/
statusCode: number;
/**
* Indicates the status of the response (false for all errors).
*/
status: boolean;
/**
* Optional error code for more specific error identification.
*/
errorCode?: string;
name: string;
message: string;
stack?: string;
};
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};