Options
Modules
  • Command
  • Localization
  • Logger
All
  • Public
  • Public/Protected
  • All
Menu

Fluent builder class used for creating reusable error handlers for different error kinds. Errors can be handled by the built error handler by calling handle

Hierarchy

  • ErrorHandler

Index

Constructors

Methods

Constructors

constructor

Methods

clearMatchers

  • clearMatchers(): void

handle

  • handle(err: Error, ...args: any[]): Promise<Result<any>>
  • Runs the given error through the matching handler function for its type. Returns Result.ok() if the error was successfully handled (Meaning a matcher exists for the error type that was given and the matched function did not throw any errors). In the event that an error is thrown in a matched function, handle() will return Result.error(), where the result value is the thrown error. In the event that no handler matches the input, Result.error() will be returned with an ErrorHandlerError which will contain the original error that was unhandled.

    Additional arguments can be passed which will be passed to the matching error handler function

    Parameters

    • err: Error
    • Rest ...args: any[]

    Returns Promise<Result<any>>

match

  • match<T>(errClass: CustomErrorConstructor<T>, handler: (err: T, ...args: any[]) => any): ErrorHandler
  • Creates a matcher for this ErrorHandler for the given error class that tells the ErrorHandler to use the given function to handle errors matching that error class. Returns this ErrorHandler.

    ErrorHandler
        .match(RangeError, err => console.log(err))
        .match(Error, err => console.log(err));
    

    If given a matcher for an Error class that already has a matcher, the existing matcher will be overridden by the newest one.

    NOTE: Matchers with lower specificity (Error being the lowest as the base Error class) should come after matchers with higher specificity, because errors that inherit from any other error class will also be matched by a matcher for the class they inherit from.

    class FooError extends Error {}
    class BarError extends FooError {}
    class BazError extends FooError {}
    
    ErrorHandler
        .match(FooError, ...)
        .match(BarError, ...)
        .match(BazError, ...)
        .handle(new BazError());
    

    Using the example above, the BazError handler will never be called because the FooError matcher has lower specificity since it has error classes that inherit from it

    Type parameters

    • T: Error

    Parameters

    • errClass: CustomErrorConstructor<T>
    • handler: (err: T, ...args: any[]) => any
        • (err: T, ...args: any[]): any
        • Parameters

          • err: T
          • Rest ...args: any[]

          Returns any

    Returns ErrorHandler

Static match

  • match<T>(errClass: CustomErrorConstructor<T>, handler: (err: T, ...args: any[]) => any): ErrorHandler
  • Creates a new ErrorHandler and assigns a matcher for the given error class that tells the ErrorHandler to use the given function to handle errors matching that error class. Returns the new ErrorHandler.

    ErrorHandler.match(RangeError, err => console.log(err));
    

    NOTE: Matchers with lower specificity (Error being the lowest as the base Error class) should come after matchers with higher specificity, because errors that inherit from any other error class will also be matched by a matcher for the class they inherit from.

    class FooError extends Error {}
    class BarError extends FooError {}
    class BazError extends FooError {}
    
    ErrorHandler
        .match(FooError, ...)
        .match(BarError, ...)
        .match(BazError, ...)
        .handle(new BazError());
    

    Using the example above, the BazError handler will never be called because the FooError matcher has lower specificity since it has error classes that inherit from it

    Type parameters

    • T: Error

    Parameters

    • errClass: CustomErrorConstructor<T>
    • handler: (err: T, ...args: any[]) => any
        • (err: T, ...args: any[]): any
        • Parameters

          • err: T
          • Rest ...args: any[]

          Returns any

    Returns ErrorHandler

Generated using TypeDoc