DeesseJS Errors

API Reference

Complete API reference for all exports from @deessejs/errors.

This page documents all public exports from @deessejs/errors. Use this as a comprehensive reference for the library's API.

error()

Creates an error factory function for defining typed, structured errors.

title=
const errorFactory = error<T>(config)

Parameters

ParameterTypeDescription
config.namestringError name identifier (required)
config.messagestringMessage template with {field} placeholders
config.fieldsStandardSchemaV1Field schema for validation (Zod, Valibot, etc.)
config.inheritsErrorFactory | ErrorFactory[]Parent error(s) to inherit from

Returns

An ErrorFactory function that creates error instances. The factory has these properties:

PropertyTypeDescription
namestringThe error name
inheritsErrorFactory | ErrorFactory[] | undefinedParent error types
schemaStandardSchemaV1 | undefinedField validation schema
rawMessagestring | undefinedOriginal message template

Example

title=
import { error } from '@deessejs/errors';

const ValidationError = error<{ field: string }>({
  name: 'ValidationError',
  message: 'Field "{field}" is invalid',
  inherits: AppError,
});

const err = ValidationError({ field: 'email' });

raise()

Throws an error instance. This is the primary mechanism for throwing errors in @deessejs/errors.

title=
raise(error: ErrorInstance): never

Parameters

ParameterTypeDescription
errorErrorInstanceThe error to throw

Returns

never — This function always throws.

Example

title=
import { error, raise } from '@deessejs/errors';

const ValidationError = error({ name: 'ValidationError' });

raise(ValidationError({}));

is()

Type guard function to check if an error is an instance of a specific error type.

title=
const result = is(error, ErrorType)

Parameters

ParameterTypeDescription
errorunknownThe error to check
ErrorTypeErrorFactory | ErrorClassThe error type to check against

Returns

booleantrue if the error matches or inherits from the type.

Example

title=
import { error, is } from '@deessejs/errors';

const AppError = error({ name: 'AppError' });
const ValidationError = error({
  name: 'ValidationError',
  inherits: AppError,
});

const err = ValidationError({});

is(err, ValidationError); // true
is(err, AppError); // true (through inheritance)

causes()

Returns all causes in an error chain.

title=
const chain = causes(error: unknown): Error[]

Parameters

ParameterTypeDescription
errorunknownThe error to get causes from

Returns

Error[] — Array of errors in the cause chain, ordered newest to oldest. Returns an empty array for null, undefined, or errors without causes.

Example

title=
import { error, causes } from '@deessejs/errors';

const AppError = error({ name: 'AppError' });
const appErr = AppError({});
appErr.from(new Error('Original error'));

const chain = causes(appErr);
console.log(chain.length); // 1

ErrorInstance

The type of object returned by error factories. It extends the native Error type.

Properties

PropertyTypeDescription
namestringError name identifier
messagestringHuman-readable error message
stackstringStack trace string
fieldsTUser-defined fields
notesstring[]Additional notes
causeError | nullDirect cause of this error
causesError[]Full cause chain
contextRecord<string, unknown> | nullInjected context data
inheritsErrorFactory | ErrorFactory[] | undefinedParent error factories

Methods

MethodDescription
from(cause: Error)Chains a cause error to this error

See Also

On this page