import type { ErrorName } from 'error-custom-class' import type { AggregateErrors } from '../merge/aggregate.js' import type { SpecificErrorInstance } from '../merge/cause.js' import type { SpecificClassOptions } from '../options/class.js' import type { Cause } from '../options/instance.js' import type { ErrorProps, MergeErrorProps } from '../plugins/core/props/main.js' import type { PluginsMixedMethods } from '../plugins/instance/mixed.js' import type { Plugins } from '../plugins/shape/main.js' import type { PluginsStaticMethods } from '../plugins/static/call.js' import type { OmitKeys } from '../utils/omit.js' import type { CustomClass, ParentExtra, ParentInstanceOptions, } from './custom.js' import type { NormalizeError } from './normalize.js' /** * `ErrorClass.subclass()` * * @private This type is private and only exported as a temporary workaround * for an open issue with TypeScript. It will be removed in a future release. * See: * * - [modern-errors issue #18](https://github.com/ehmicky/modern-errors/issues/18) * - [TypeScript issue #47663](https://github.com/microsoft/TypeScript/issues/47663) */ type CreateSubclass< PluginsArg extends Plugins, ErrorPropsArg extends ErrorProps, CustomClassArg extends CustomClass, > = < ChildPlugins extends Plugins = [], ChildCustomClass extends CustomClassArg = CustomClassArg, ChildProps extends ErrorProps = object, >( errorName: ErrorName, options?: SpecificClassOptions< PluginsArg, ChildPlugins, ChildProps, ChildCustomClass >, ) => SpecificErrorClass< [...PluginsArg, ...ChildPlugins], MergeErrorProps, ChildCustomClass > /** * Non-dynamic members of error classes * * @private This type is private and only exported as a temporary workaround * for an open issue with TypeScript. It will be removed in a future release. * See: * * - [modern-errors issue #18](https://github.com/ehmicky/modern-errors/issues/18) * - [TypeScript issue #47663](https://github.com/microsoft/TypeScript/issues/47663) */ interface ErrorSubclassCore< PluginsArg extends Plugins, ErrorPropsArg extends ErrorProps, CustomClassArg extends CustomClass, > { /** * Error subclass * * @example * ```js * throw new InputError('Missing file path.') * ``` */ new < ChildProps extends ErrorProps = ErrorProps, AggregateErrorsArg extends AggregateErrors = AggregateErrors, CauseArg extends Cause = Cause, >( message: string, options?: ParentInstanceOptions< PluginsArg, ChildProps, CustomClassArg, AggregateErrorsArg, CauseArg >, ...extra: ParentExtra ): SpecificErrorInstance< PluginsArg, MergeErrorProps, CustomClassArg, AggregateErrorsArg, CauseArg > readonly prototype: InstanceType< ErrorSubclassCore > /** * Creates and returns a child `ErrorClass`. * * @example * ```js * export const InputError = ErrorClass.subclass('InputError', options) * ``` */ subclass: CreateSubclass /** * Normalizes invalid errors. * * If `error` is an instance of `ErrorClass` (or one of its subclasses), it is * left as is. Otherwise, it is converted to `NewErrorClass`, which defaults * to `ErrorClass` itself. * * @example * ```js * try { * throw 'Missing file path.' * } catch (invalidError) { * const normalizedError = BaseError.normalize(invalidError) * // This works: 'Missing file path.' * // `normalizedError` is a `BaseError` instance. * console.log(normalizedError.message.trim()) * } * ``` */ normalize: NormalizeError } /** * Error class, with specific `props`, `custom`, etc. * * @private This type is private and only exported as a temporary workaround * for an open issue with TypeScript. It will be removed in a future release. * See: * * - [modern-errors issue #35](https://github.com/ehmicky/modern-errors/issues/35) * - [modern-errors issue #18](https://github.com/ehmicky/modern-errors/issues/18) * - [TypeScript issue #47663](https://github.com/microsoft/TypeScript/issues/47663) */ export type SpecificErrorClass< PluginsArg extends Plugins, ErrorPropsArg extends ErrorProps, CustomClassArg extends CustomClass, > = ErrorSubclassCore & OmitKeys< CustomClassArg, keyof ErrorSubclassCore > & PluginsStaticMethods & PluginsMixedMethods /** * Error class */ export type ErrorClass = SpecificErrorClass