Skip to content

p-error

The error module provides functions for creating and inspecting JavaScript errors.

use p-error as e

Creates a generic Error.

Creates a TypeError.

Creates a RangeError.

Creates a ReferenceError.

Creates a SyntaxError.

Creates a URIError.

Creates an Error with a cause (for error chaining).

use p-error as e
throw e.create[///something went wrong///]
throw e.type[///expected a string///]
throw e.range[///index out of bounds///]
-- Error chaining
try
riskyOperation[]
catch err
throw e.wrap[///operation failed///; err]

Returns true if val is an instance of Error.

Returns the error message. If err is not an Error, returns String(err).

Returns the error name (e.g. "TypeError", "RangeError"). Returns "Error" for non-Error values.

Returns the stack trace as a string. Returns "" for non-Error values.

Returns the error’s cause (if set via wrap or the cause option).

use p-error as e
const err be e.type[///invalid argument///]
e.iserror[err] -- true
e.message[err] -- "invalid argument"
e.name[err] -- "TypeError"
e.stack[err] -- "TypeError: invalid argument\n at ..."
const wrapped be e.wrap[///failed///; err]
e.cause[wrapped] -- the original TypeError
e.message[wrapped] -- "failed"