Removes all currently registered matchers from this ErrorHandler
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
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
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
Generated using TypeDoc
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