UNPKG

@storm-stack/errors

Version:

This package includes a base error class and various utility functions for working with errors.

168 lines (167 loc) 6.35 kB
import { ErrorMessageDetails, ValidationDetails } from "@storm-stack/types"; import { ErrorType, type StormErrorOptions } from "./types"; /** * Creates a new StormError instance * * @param cause - The cause of the error * @returns The newly created StormError */ export declare function createStormError<TCode extends string = string, TErrorType extends ErrorType = ErrorType, TData = undefined>({ code, name, type, message, cause, stack, data }: StormErrorOptions<TErrorType, TData> & { code?: TCode; }): StormError<TCode, TErrorType, TData>; /** * Gets the cause of an unknown error and returns it as a StormError * * @param cause - The cause of the error in an unknown type * @returns The cause of the error in a StormError object or undefined */ export declare function getCauseFromUnknown<TErrorType extends ErrorType = ErrorType, TData = undefined>(cause: unknown, type: TErrorType | undefined, data: TData): StormError<string, TErrorType, TData>; /** * Type-check to determine if `obj` is a `StormError` object * * @param value - the object to check * @returns The function isStormError is returning a boolean value. */ export declare function isStormError<TCode extends string = string>(value: unknown): value is StormError<TCode>; /** * A wrapper around the base JavaScript Error class to be used by Storm Software * * @decorator `@Serializable()` */ declare class StormError<TCode extends string = string, TErrorType extends ErrorType = typeof ErrorType.EXCEPTION, TData = undefined> extends Error { __proto__: any; /** * The stack trace */ private _stack?; /** * The inner error */ private _cause?; /** * The error code */ code: TCode; /** * Additional data to be passed with the error */ data: TData; /** * The type of error response message/event */ type: TErrorType; /** * Creates a new StormError instance * * @param error - The error to create * @returns The newly created StormError */ static create<TErrorType extends ErrorType = ErrorType, TData = undefined>(error?: unknown, type?: TErrorType, data?: TData): StormError<string, TErrorType, TData>; /** * Creates a new Exception StormError instance * * @param error - The validation details * @param data - The options to use * @returns The newly created StormError */ static createException<TData = undefined>(error?: unknown, data?: TData): StormError<string, typeof ErrorType.EXCEPTION, TData>; /** * Creates a new Exception StormError instance * * @param data - The options to use * @returns The newly created StormError */ static createFromData<TData = undefined>(data?: TData): StormError<string, typeof ErrorType.EXCEPTION, TData>; /** * Creates a new Validation StormError instance * * @param validationDetails - The validation details * @param options - The options to use * @returns The newly created StormError */ static createValidation(validationDetails: ValidationDetails | ValidationDetails[], options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.VALIDATION, ValidationDetails[]>; /** * Creates a new Not Found StormError instance * * @param recordName - The name of the items returned (or in this case *not returned*) in the search results * @param options - The options to use * @returns The newly created StormError */ static createNotFound(recordName?: string, options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.NOT_FOUND, string | undefined>; /** * Creates a new Security StormError instance * * @param data - Any relevant data related to the security issue * @param options - The options to use * @returns The newly created StormError */ static createSecurity(data?: any, options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.NOT_FOUND, any>; /** * Creates a new Service Unavailable StormError instance * * @param serviceName - The name of the service that is currently unavailable * @param options - The options to use * @returns The newly created StormError */ static createServiceUnavailable(serviceName: string, options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.SERVICE_UNAVAILABLE, string>; /** * Creates a new Action Unsupported StormError instance * * @param action - The action that is unsupported * @param options - The options to use * @returns The newly created StormError */ static createActionUnsupported(action: string, options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.ACTION_UNSUPPORTED, string>; /** * Creates a new Unknown StormError instance * * @param data - The action that is unsupported * @param options - The options to use * @returns The newly created StormError */ static createUnknown(data: any, options?: Omit<StormErrorOptions, "type" | "data">): StormError<string, typeof ErrorType.UNKNOWN, any>; constructor(code: TCode, options: StormErrorOptions<TErrorType, TData>); /** * The cause of the error */ get cause(): StormError | undefined; /** * The cause of the error */ set cause(_cause: unknown); /** * Prints a displayable/formatted stack trace * * @returns The stack trace string */ get stack(): string; /** * Store the stack trace */ set stack(_stack: string); /** * The unformatted stack trace * * @returns The stack trace string */ get originalStack(): string; /** * Prints the display error message string * * @returns The display error message string */ print(): string; /** * Prints the error message and stack trace * * @returns The error message and stack trace string */ toString(stacktrace?: boolean): string; /** * Convert the error object into a message details object * * @returns The error message details object */ toMessage(): ErrorMessageDetails; } export { StormError };