@belgattitude/http-exception
Version:
Warning: has been moved to @httpx/exception. Please update.
133 lines (122 loc) • 4.53 kB
TypeScript
type HttpExceptionParams = {
/**
* Exception message, if not provided the default is the exception
* name in natural language (ie: "HttpNotFound" -> "Not found")
*/
message?: string;
/**
* Indicates the original url that caused the error.
*/
url?: string;
/**
* Inform about http method
*/
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
/**
* Custom additional code (ie: 'AbortError', 'CODE-1234'...)
*/
code?: string;
/**
* Inform about an unique error identifier (ie: nanoid, cuid...)
*/
errorId?: string;
/**
* Indicates the original cause of the HttpException.
* Will be ignored/discarded if the runtime (browser / node version) does not support it
* or there's no polyfill
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*/
cause?: Error;
};
type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
declare class HttpException extends Error {
/**
* Http error status code (400-599)
*/
readonly statusCode: number;
/**
* Indicates the original url that caused the error.
*/
readonly url: string | undefined;
/**
* Http method
*/
readonly method: HttpMethod | undefined;
/**
* Custom additional code (ie: 'AbortError', 'CODE-1234'...)
*/
readonly code: string | undefined;
/**
* Inform about an unique error identifier (ie: nanoid, cuid...)
*/
readonly errorId: string | undefined;
/**
* If set and the runtime (browser or node) supports it
* you can get back the error cause
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*/
readonly cause?: Error | HttpException;
/**
* Construct a new HttpException class
*
* @param statusCode http status code between 400-599, no checks are done on the validity of the number.
* @param msgOrParams either a message or an object containing HttpExceptionParams
*/
constructor(statusCode: number, msgOrParams?: HttpExceptionParams | string);
}
declare class SerializerError extends Error {
constructor(message: string, params?: {
cause?: Error;
});
}
declare const fromJson: (json: string) => Error | HttpException | SerializerError;
/**
* Supported native ecmascript errors
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#error_types
* @see https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
*/
type NativeError = Error | EvalError | RangeError | ReferenceError | SyntaxError | TypeError | URIError;
type DiscriminateSerializable<T extends Serializable['__type']> = Extract<Serializable, {
__type: T;
}>;
type NativeErrorFields = {
/** Class name, ie: Error, RangeError, HttpException, HttpBadRequest */
name: string;
/** Error message (a string, non-empty with HttpException subclasses) */
message: string;
stack?: string;
cause?: Serializable;
};
type HttpExceptionFields = NativeErrorFields & {
statusCode: number;
url?: string;
method?: HttpMethod;
errorId?: string;
code?: string;
};
type Serializable = ({
__type: 'NonNativeError';
} & NativeErrorFields) | ({
__type: 'NativeError';
} & NativeErrorFields) | ({
__type: 'HttpException';
} & HttpExceptionFields);
type SerializableHttpException = DiscriminateSerializable<'HttpException'>;
type SerializableError = DiscriminateSerializable<'NativeError'>;
type SerializableNonNativeError = DiscriminateSerializable<'NonNativeError'>;
declare const toJson: (exception: Error | NativeError | HttpException) => string;
/**
* Convert an Error, NativeError or any HttpException to
* an object suitable for serialization (a serializable version).
*
* @link {createFromSerializable}
*/
declare const convertToSerializable: (e: Error | NativeError | HttpException) => Serializable;
/**
* create an Error, NativeError or any HttpException from a
* serializable representation
*
* @link {convertToSerializable}
*/
declare const createFromSerializable: (payload: Serializable) => HttpException | NativeError | Error;
export { NativeError, SerializableError, SerializableHttpException, SerializableNonNativeError, SerializerError, convertToSerializable, createFromSerializable, fromJson, toJson };