@altostra/core
Version:
Core library for shared types and logic
112 lines (111 loc) • 5.52 kB
TypeScript
/// <reference types="node" />
import type { InspectOptions } from 'util';
import { inspect } from 'util';
import type { Maybe } from "../Maybe";
import type { ErrorHandler, ErrorLike } from "./Common";
import { VerboseMessage } from "./VerboseMessage";
export interface ErrorData<TErrorCode = never, TErrorData = never> {
/**
* An optional error message, or a function from the original error to an error message
*/
message?: ErrorHandler<string>;
/**
* An optional function from an error to error code
*/
code?: ErrorHandler<TErrorCode>;
/**
* An optional function from an error to error data
*/
data?: ErrorHandler<TErrorData>;
/**
* To to wrap a thrown error, even if it is already an AltoError
*/
forceWrap?: boolean;
}
export interface AltoErrorData<TErrorCode = never, TErrorData = never> {
message: string;
code?: TErrorCode;
data?: TErrorData;
}
/**
* An error with additional data, that can be used to wrap other errors, and preserve the data embedded in them
*/
export declare class AltoError<TErrorCode = never, TErrorData = never> extends Error {
#private;
private static _inspectOptions;
readonly inner?: Error;
readonly code?: TErrorCode;
readonly data?: TErrorData;
/**
* Creates new `AltoError` instance.
* @param message The error message.\
* If the message is `undefined`, and an `inner` error is provided, the message is taken from `inner`.
* @param inner An optional error that triggered the creation of this `AltoError`.
* @param code An optional code that identifies this error
* @param data An optional additional data to save alongside the created `AltoError`
*/
constructor(message: string | undefined, inner?: Error, code?: TErrorCode, data?: TErrorData);
get fullMessage(): string;
get fullStack(): Maybe<string>;
get verboseMessage(): VerboseMessage;
setInspectionOptions(opt: Maybe<InspectOptions>): void;
resetInspectionOptions(): void;
private get _inspectOptions();
[inspect.custom](depth: number): string;
protected get _classInspectionOptions(): Maybe<InspectOptions>;
/**
* Creates new `AltoError`, having the provided message.
* @param message The error message of the *wrapping* `AltoError`.
* @param params Optional params that include and optional code, and optional data.
* @returns A new `AltoError` instance, having the provided message.
*/
static create<TErrorCode = never, TErrorData = never>(message: string, params?: Omit<AltoErrorData<TErrorCode, TErrorData>, 'message'>): AltoError<TErrorCode, TErrorData>;
/**
* Creates new `AltoError` with the provided parameters.
* @param params Parameters for creating the `AltoError` instance.
* @returns A new `AltoError` instance, having the provided message.
*/
static create<TErrorCode = never, TErrorData = never>(params: AltoErrorData<TErrorCode, TErrorData>): AltoError<TErrorCode, TErrorData>;
/**
* Wraps any error that might be thrown from func, with an AltoError error
* @param {Function} func A function that may throw an error
* @param {ErrorData} errData Data for creating AltoError if the given function would throw
* @returns A new function with the same signature as `func` but throws `AltoError`
* on failures instead of anything else.
*/
static wrapFunction<T extends (...args: unknown[]) => unknown, TErrorCode = never, TErrorData = never>(func: T, errData?: ErrorData<TErrorCode, TErrorData>): T;
/**
* Executing an action, and wrapping any thrown error from it
* @param action The action to wrap
* @param {ErrorData} errData Data for creating AltoError if the given function would throw
* @returns The action result
*/
static wrapActionExecution<T, TErrorCode = never, TErrorData = never>(action: () => T, errData?: ErrorData<TErrorCode, TErrorData>): T;
/**
* Wraps an error with an `AltoError` error
* @param err An error to wrap with an `AltoError`
* @param {ErrorData} errData Data for creating `AltoError`
* @returns An `AltoError` instance that wraps the original error.
*/
static wrapError<TErrorCode = never, TErrorData = never>(err: unknown, errData?: ErrorData<TErrorCode, TErrorData>): AltoError<TErrorCode, TErrorData>;
/**
* Wraps any error that might cause rejection of promise, with an `AltoError` error \
* Returns new promise that would be rejected with an `AltoError`
* @param {Promise} promise A promise that may be rejected with an error
* @param {ErrorData} errData Data for creating `AltoError` if the given function would throw
* @returns A promise that rejected when the original `promise` is rejected, but the rejection
* reason is an `AltoError` that wraps the original `promise` rejection reason.
*/
static wrapPromise<T, TErrorCode = never, TErrorData = never>(promise: Promise<T>, errData?: ErrorData<TErrorCode, TErrorData>): Promise<T>;
static setInspectionOptions(opt: Maybe<InspectOptions>): void;
static resetInspectionOptions(): void;
}
export declare type ErrorMessage<TErr> = TErr extends ErrorLike ? string : Maybe<string>;
/**
* Extract an error message from a thrown value/
* @param err A thrown error
* @return A error message if such could be extracted.
*/
export declare function getErrorMessage(err: any): Maybe<string>;
export declare function getFullErrorMessage<TErr>(err: TErr): ErrorMessage<TErr>;
export declare function getFullStack(err: any): Maybe<string>;