UNPKG

conway-errors

Version:

A convenient primitive for creating, structing and throwing errors

67 lines (66 loc) 3.4 kB
/** * Root interface for any specific error type. */ export interface IConwayError extends Error { rootContext: string; contextsChunk: string; feature: string; originalError?: OriginalError; extendedParams?: ExtendedParams; emit: EmitFn; } type EmitFn = (extendedParams?: ExtendedParams) => void; /** * Type guard which helps to understand if error is Conway error. * @param error * @return {boolean} */ export declare function isConwayError(error: unknown): error is IConwayError; type ExtendedParams = Record<string, unknown>; type OriginalError = Error | Record<string, unknown> | unknown; interface CreateErrorOptions { handleEmit?: (err: IConwayError, extendedParams?: ExtendedParams) => void; extendedParams?: ExtendedParams; } type ErrorTypeConfig = ReadonlyArray<{ errorType: string; createMessagePostfix?: (originalError?: OriginalError) => string; }>; type ErrorFnOptions = { originalError?: OriginalError; extendedParams?: ExtendedParams; }; type CreateErrorFn<ErrorType extends string> = (errorType: ErrorType, message: string, options?: ErrorFnOptions) => IConwayError; type Brand<T, B> = T & { __brand: B; }; type ErrorSubcontext<Name extends string, ErrorType extends string> = Brand<Subcontext<Name, ErrorType>, Name>; type ErrorFeature<Name extends string, ErrorType extends string> = Brand<CreateErrorFn<ErrorType>, Name>; export type AnyFeatureOfSubcontext<S> = S extends ErrorSubcontext<infer Name, infer ErrorType> ? ErrorFeature<`${Name}/${string}`, ErrorType> : never; type Subcontext<Name extends string, ErrorType extends string> = { /** * Create a child context within the current context. * * @param {string} childContextName - The name of the child context. * @param {ExtendedParams} extendedParams - Additional extended parameters for the child context. * @return {Function} Function to create an error context with the specified child context name and extended params. */ subcontext: <const ChildContextName extends string>(subcontextName: ChildContextName, extendedParams?: ExtendedParams) => ErrorSubcontext<`${Name}/${ChildContextName}`, ErrorType>; /** * Creates a child feature within the current context. * * @param {string} childFeatureName - The name of the child feature. * @param {ExtendedParams} [extendedParams={}] - Additional extended parameters for the child feature. * @return {Function} The created error feature. */ feature: <const FeatureName extends string>(featureName: FeatureName, featureContextExtendedParams?: ExtendedParams) => ErrorFeature<`${Name}/${FeatureName}`, ErrorType>; }; /** * Function to create an error context with specified error types and options. * * @param {ErrorTypeConfig} errorTypes - Array of error types and optional message postfix creation functions. * @param {CreateErrorOptions} options - Options for error creation, including custom throw function and extended params. * @return {Function} Function to create an error context with specific context name and extended params. */ export declare function createError<ErrorTypes extends ErrorTypeConfig>(errorTypes?: ErrorTypes, options?: CreateErrorOptions): <const ContextName extends string>(contextName: ContextName, extendedParams?: ExtendedParams) => ErrorSubcontext<ContextName, ErrorTypes[number]["errorType"]>; export {};