@xylabs/error
v5.0.87
Published
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Readme
@xylabs/error
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Reference
@xylabs/error
Type Aliases
| Type Alias | Description | | ------ | ------ | | AssertConfig | Configuration for assertion behavior: a static message string, a boolean toggle, or a callback. |
Functions
| Function | Description | | ------ | ------ | | assertError | Throws an Error based on the assert configuration when a value fails validation. | | handleError | Invokes the handler if the value is an Error, otherwise re-throws it. | | handleErrorAsync | Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it. | | isError | Type guard that checks whether a value is an Error instance. |
functions
assertError
function assertError(
value: unknown,
assert: AssertConfig | undefined,
defaultMessage: string): undefined;Throws an Error based on the assert configuration when a value fails validation.
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| value | unknown | The value being validated |
| assert | AssertConfig | undefined | Assertion config controlling the error message |
| defaultMessage | string | Fallback message if no custom message is provided |
Returns
undefined
handleError
function handleError<T>(error: any, handler: (error: Error) => T): T;Invokes the handler if the value is an Error, otherwise re-throws it.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| error | any | The caught value to inspect |
| handler | (error: Error) => T | Callback invoked with the Error if it is one |
Returns
T
The handler's return value
handleErrorAsync
function handleErrorAsync<T>(error: any, handler: (error: Error) => Promise<T>): Promise<T>;Async version of handleError. Invokes the async handler if the value is an Error, otherwise re-throws it.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| error | any | The caught value to inspect |
| handler | (error: Error) => Promise<T> | Async callback invoked with the Error if it is one |
Returns
Promise<T>
The handler's resolved return value
isError
Call Signature
function isError(value: unknown): value is Error;Type guard that checks whether a value is an Error instance.
Parameters
| Parameter | Type |
| ------ | ------ |
| value | unknown |
Returns
value is Error
Call Signature
function isError<T>(value: T): value is Extract<T, Error>;Type guard that checks whether a value is an Error instance.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is Extract<T, Error>
type-aliases
AssertConfig
type AssertConfig = string | AssertCallback | boolean;Configuration for assertion behavior: a static message string, a boolean toggle, or a callback.
Part of sdk-js
Maintainers
License
See the LICENSE file for license details
