ufiber
Version:
Next-gen webserver for node-js developer
260 lines (259 loc) • 7.75 kB
TypeScript
import { ClientErrorStatusCode, ServerErrorStatusCode } from "./status.js";
//#region src/errors.d.ts
/** The type for the body message of HTTP errors. */
type Message = string | string[];
/** The structure of the HTTP error body. */
type ErrorBody = {
data?: Record<string, any> | null;
code?: string | null;
error: string;
status: Status;
message: Message;
};
type Status = ServerErrorStatusCode | ClientErrorStatusCode;
/**
* Base class for handling HTTP errors.
*/
declare class HttpError extends Error {
readonly status: Status;
readonly options: Pick<ErrorBody, 'message' | 'data' | 'code'> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
/**
* Creates an instance of `HttpError`.
*/
constructor(status?: Status, options?: Pick<ErrorBody, 'message' | 'data' | 'code'> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
});
static isError(value: unknown): value is HttpError;
getBody(): ErrorBody;
}
/**
* Utility to create custom HttpError subclasses with optional custom naming.
*/
declare const createHttpError: (status: Status, defaultName?: string) => {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents a Bad Request HTTP error (400).
* @extends {HttpError}
*/
declare const BadRequestError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents a Conflict HTTP error (409).
* @extends {HttpError}
*/
declare const ConflictError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents a Forbidden HTTP error (403).
* @extends {HttpError}
*/
declare const ForbiddenError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents a Not Found HTTP error (404).
* @extends {HttpError}
*/
declare const NotFoundError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents an UnAuthorized HTTP error (401).
* @extends {HttpError}
*/
declare const UnAuthorizedError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents an Internal Server Error HTTP error (500).
* @extends {HttpError}
*/
declare const InternalServerError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
/**
* Represents an Content Too Larger Error HTTP error (413).
* @extends {HttpError}
*/
declare const ContentTooLargeError: {
new (message: Message, options?: {
cause?: unknown;
code?: string | null;
data?: Record<string, unknown> | null;
name?: string;
}): {
readonly status: Status;
readonly options: Pick<ErrorBody, "message" | "data" | "code"> & {
/** Optional custom name override for the error */
name?: string;
cause?: unknown;
};
getBody(): ErrorBody;
name: string;
message: string;
stack?: string;
cause?: unknown;
};
isError(value: unknown): value is HttpError;
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
stackTraceLimit: number;
};
//#endregion
export { BadRequestError, ConflictError, ContentTooLargeError, ForbiddenError, HttpError, InternalServerError, NotFoundError, UnAuthorizedError, createHttpError };