Error
The @perfective/error
package helps organize exceptions and error handling.
It defines an Exception
, based on the JS
Error
class,
that supports localizable error messages and error chaining;
and provides functions to handle error stack output.
Read the full documentation in the repository.
-
Exception
:-
Exception.toString()
— outputs the stack of all error messages. -
exception(message: string, tokens: ExceptionTokens = {}, context: ExceptionContext = {}): Exception
— creates anException
without a previous error. -
causedBy(previous: Error, message: string, tokens: ExceptionTokens = {}, context: ExceptionContext = {}): Exception
— creates anException
with a previous error. -
unknownError(error: unknown): Error | Exception
— returns a passedError
or creates a newLiteral error
Exception
. -
isException<T>(value: Exception | T): value is Exception
— returnstrue
when value is anException
. -
isNotException<T>(value: Exception | T): value is T
— returnstrue
when value is not anException
. -
chainStack(error: Error): string
— outputs the stack of all errors with their stack trace. -
fault(error: Error): Error
— returns the first error in the chain.
-
-
ExceptionMessage
:-
exceptionMessage(template: string, tokens?: ExceptionTokens): ExceptionMessage
— creates anExceptionMessage
with an empty tokens object if the argument not provided. -
exceptionMessageOutput(message: ExceptionMessage): string
— creates a string to output anExceptionMessage
.
-
-
Failure
— a record type to serialize errors and exceptions:-
failure<E extends Error>(error: E): Failure
— convert andError
or anException
into aFailure
record.
-
-
Throwing errors:
-
throws<E extends Error>(error: E): never
— throws a provided error. -
throws<E extends Error>(error: () => E): never
— throws an error created by the provided function. -
throws<E extends Error>(message: string, context: ExceptionContext = {}): never
— creates and throws anException
with the givenmessage
andcontext
. -
rethrows(previous: Error, message: string, context: ExceptionContext = {}): never
— creates and throws an exception with the givenmessage
,context
, andprevious
error. -
Panic
— a type of nullary functions that throw an error when executed:-
panic<E extends Error>(error: E): Panic
— creates a function that throws a provided error. -
panic<E extends Error>(error: () => E): Panic
— creates a function that throws an error created by the provided function. -
panic<E extends Error>(message: string, context: ExceptionContext = {}): Panic
— creates a function that throws anException
with the givenmessage
andcontext
.
-
-
Rethrow<E extends Error = Error>
— a type of unary functions that rethrow a previous error when executed:-
rethrow(message: string, context: ExceptionContext = {}): Rethrow
— creates a function that throws an exception with the givenmessage
,context
, andprevious
error.
-
-
-
-
error(message?: string): Error
— instantiates a newError
. -
isError<T>(value: Error | T): value is Error
— returnstrue
when the value is an instance ofError
. -
isNotError<T>(value: Error | T): value is T
— returnstrue
when the value is not an instance ofError
.
-
-
-
evalError(message?: string): EvalError
— instantiates a newEvalError
. -
isEvalError<T>(value: EvalError | T): value is EvalError
— returnstrue
when the value is an instance ofEvalError
. -
isNotEvalError<T>(value: EvalError | T): value is T
— returnstrue
when the value is not an instance ofEvalError
.
-
-
-
rangeError(message?: string): RangeError
— instantiates a newRangeError
. -
isRangeError<T>(value: RangeError | T): value is RangeError
— returnstrue
when the value is an instance ofRangeError
. -
isNotRangeError<T>(value: RangeError | T): value is T
— returnstrue
when the value is not an instance ofRangeError
.
-
-
-
referenceError(message?: string): ReferenceError
— instantiates a newReferenceError
. -
isReferenceError<T>(value: ReferenceError | T): value is ReferenceError
— returnstrue
when the value is an instance ofReferenceError
. -
isNotReferenceError<T>(value: ReferenceError | T): value is T
— returnstrue
when the value is not an instance ofReferenceError
.
-
-
-
syntaxError(message?: string): SyntaxError
— instantiates a newSyntaxError
. -
isSyntaxError<T>(value: SyntaxError | T): value is SyntaxError
— returnstrue
when the value is an instance ofSyntaxError
. -
isNotSyntaxError<T>(value: SyntaxError | T): value is T
— returnstrue
when the value is not an instance ofSyntaxError
.
-
-
-
typeError(message?: string): TypeError
— instantiates a newTypeError
. -
isTypeError<T>(value: TypeError | T): value is TypeError
— returnstrue
when the value is an instance ofTypeError
. -
isNotTypeError<T>(value: TypeError | T): value is T
— returnstrue
when the value is not an instance ofTypeError
.
-