Skip to content

API ​

Every function takes unknown, so the catch variable can be passed straight in. None of them throw, whatever the value.

toError(value) ​

Converts any caught value to an Error.

valueResult
An Error, including one from another realmThe same Error, unmodified
An object with a string messageA new Error with the object's message, name, stack and cause, and its other own properties copied over
Anything elseA new NonError describing it

Returns Error. See Converting caught values.

getErrorMessage(value) ​

The message of toError(value).

Returns string. A message that isn't a string is stringified, and one whose getter throws gives '[unreadable message]'.

hasCode(value, code) ​

Whether value is an Error whose code property equals code.

ParameterTypeDefaultDescription
valueunknown—A caught value.
codestring | number—The code to match, like 'ENOENT'.

Returns boolean, narrowing value to Error & { readonly code: typeof code }:

ts
if (
hasCode
(
err
, 'ENOENT')) {
console
.
log
(
err
);
const err: Error & {
    readonly code: "ENOENT";
}
}

Only real Errors match. To match error-like objects too, use hasCode(toError(err), code).

NonError ​

class NonError extends Error. Wraps a thrown value that was neither an Error nor shaped like one.

new NonError(thrown, options?) ​

ParameterTypeDefaultDescription
thrownunknown—The value that was thrown.
optionsErrorOptions—Standard Error options, such as cause.
PropertyTypeDescription
namestring'NonError'.
messagestringA description of thrown, cut to 1,000 characters. See Everything else.
thrownunknownThe original value.

walkErrors(value, options?) ​

Visits value, its cause chain and the errors of any AggregateError, depth first: an error, then its cause, then its errors in order. Each value is converted with toError, and a value already visited is skipped.

ParameterTypeDefaultDescription
valueunknown—A caught value.
optionsWalkOptions{}

Returns Generator<ErrorNode, void, undefined>. See Cause chains.

formatError(value, options?) ​

Formats an error with every cause and AggregateError entry, each indented under its parent. Causes are labelled Caused by: and AggregateError entries -.

ParameterTypeDefaultDescription
valueunknown—A caught value.
optionsWalkOptions{}

Returns string. Each error is printed as its stack, with a Name: message line added first when the stack doesn't start with one. Where maxDepth cut the walk short, a … more causes omitted line says so.

Types ​

WalkOptions ​

OptionTypeDefaultDescription
maxDepthnumber32Deepest level to visit. The root is level 0. Infinity for no limit.

ErrorNode ​

PropertyTypeDescription
errorErrorThe value, converted with toError.
depthnumberDistance from the root, which is 0.
relationErrorRelationHow it relates to its parent.
truncatedbooleantrue when maxDepth stopped the walk here, so this error's own cause and errors were skipped.

ErrorRelation ​

'root' | 'cause' | 'aggregate': the value passed in, its parent's cause, or an entry in its parent's errors.