import { BaseError as CustomError } from 'make-error' ; import { retry , RetryOptions } from './retry' ; import { ignore , IgnoreFunc } from './ignore' ; /**
 * Base error class.
 */ export class BaseError < A > extends CustomError { param message param attrs constructor ( message ? , attrs ? ) { super ( message ) ; Object . assign ( this , attrs ) ; } param message param attrs example static makeAssert < T , A > ( message , attrs ? ) { return ( value ) => this . assert ( value , message , attrs ) ; } param data param message param attrs static assert < T , A > ( data , message ? , attrs ? ) { if ( ! data ) { throw new this ( message , attrs ) ; } return data as NonFalsy < T > ; } param fn param opts static retry < T > ( fn , opts ? ) { return retry ( ( again , attempt ) => { return Promise . resolve ( ) . then ( ( ) => fn ( attempt ) ) . catch ( ( err ) => { if ( err instanceof this ) { throw again ( err ) ; } throw err ; } ) ; } , opts ) ; } /**
     * Makes an ignore function for this error class.
     */ static ignore ( ) ; param valueToReturnOnCatch static ignore < T > ( valueToReturnOnCatch ) ; param valueToReturnOnCatch static ignore ( valueToReturnOnCatch ? ) { return ignore ( this ) ( valueToReturnOnCatch ) ; } /**
     * Default toJSON implementation.
     */ toJSON ( ) { const result = { stack : this . stack , message : this . message , } ; for ( const prop in this ) { const val = this [ prop ] as any ; if ( typeof val !== 'function' ) { result [ prop ] = val ; } } return result ; } } /**
 * Asserter function.
 */ export type Asserter < T > = ( value : T ) => NonFalsy < T > ; /**
 * Non-falsy value.
 */ export type NonFalsy < T > = Exclude < T , false | 0 | undefined | null | '' > ; /**
 * Error attributes.
 */ export type ErrorAttributes < A > = A & { [ key ] : any ; } ; /**
 * Base error constructor.
 */ export interface BaseErrorConstructor < A > { /**
     * Constructs the error.
     */ new ( message ? , attrs ? ) : BaseError < A > & ErrorAttributes < A > ; /**
     * Makes an asserter function.
     */ makeAssert < T > ( message , attrs ? ) : Asserter < T > ; /**
     * Asserts the truthiness of the given value, throws the error otherwise.
     */ assert < T > ( data , message ? , attrs ? ) : NonFalsy < T > ; /**
     * Retries the function until it does not throw the error type.
     */ retry < T > ( fn , opts ? ) : Promise < T > ; /**
     * Makes an ignore function for this error class.
     */ ignore ( ) : IgnoreFunc < undefined > ; param valueToReturnOnCatch ignore < T > ( valueToReturnOnCatch ) : IgnoreFunc < T > ; param valueToReturnOnCatch ignore ( valueToReturnOnCatch ? ) : IgnoreFunc < any > ; } param defaultMessage param defaultAttrs export function MakeErrorClass < A = { } > ( defaultMessage = 'An error occured' , defaultAttrs ? ) { class ConfiguredError extends BaseError < A > { constructor ( message ? , attrs ? ) { message = message === null || message === undefined ? defaultMessage : message ; super ( message , Object . assign ( { } , defaultAttrs , attrs ) ) ; } } return ( ConfiguredError as unknown ) as BaseErrorConstructor < A > ; }