Skip to main content

AnyError

@ts-rust/std


@ts-rust/std / Error / AnyError

Class: AnyError<T>

Defined in: packages/std/src/error/any.error.ts:26

A generic error class extending Error with a typed kind and optional reason.

This class provides a structured way to represent errors with a category (kind) of a primitive type (e.g., string, number, enum) and an optional underlying cause (reason). The error message is automatically formatted to include both the kind and reason (if provided), making it suitable for categorized error handling in libraries or applications.

Example

const err1 = new AnyError("Invalid input", "ValidationError");
const err2 = new AnyError("File not found", 404, new Error("ENOENT"));

expect(err1.message).toBe("[ValidationError] Invalid input.");
expect(err2.message).toBe("[404] File not found. Reason: ENOENT");
expect(err2.kind).toBe(404);
expect(err2.reason.message).toBe("ENOENT");

Extends

  • Error

Extended by

Type Parameters

Type ParameterDescription

T extends Primitive

The type of the error kind, constrained to Primitive (e.g., string, number, enum).

Constructors

Constructor

new AnyError<T>(
message,
kind,
reason?): AnyError<T>;

Defined in: packages/std/src/error/any.error.ts:56

Constructs a new AnyError instance with a message, kind, and optional reason.

The error’s message is formatted as [kind] message or [kind] message. Reason: reason if a reason is provided. The name is set to the constructor’s name, and the reason is normalized to an Error instance.

Parameters

ParameterTypeDescription

message

string

The descriptive message for the error.

kind

T

The category or type of the error, a primitive value.

reason?

unknown

An optional underlying cause, which can be any value (converted to Error if not already).

Returns

AnyError<T>

Overrides

Error.constructor;

Properties

PropertyModifierTypeDescriptionInherited fromDefined in

cause?

public

unknown

Error.cause;

node_modules/.pnpm/typescript@5.8.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26

kind

readonly

T

The category or type of the error, represented as a primitive value.

This readonly property identifies the error’s kind, such as a string code or numeric status, and is set during construction.

packages/std/src/error/any.error.ts:33

message

public

string

Error.message;

node_modules/.pnpm/typescript@5.8.3/node_modules/typescript/lib/lib.es5.d.ts:1077

name

public

string

Error.name;

node_modules/.pnpm/typescript@5.8.3/node_modules/typescript/lib/lib.es5.d.ts:1076

reason

readonly

Error

The underlying cause of the error, represented as an Error instance.

This readonly property holds the reason provided during construction, normalized to an Error object. If no reason is given, it defaults to an error wrapping the kind.

packages/std/src/error/any.error.ts:42

stack?

public

string

Error.stack;

node_modules/.pnpm/typescript@5.8.3/node_modules/typescript/lib/lib.es5.d.ts:1078

prepareStackTrace?

static

(err, stackTraces) => any

Optional override for formatting stack traces

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Error.prepareStackTrace;

node_modules/.pnpm/@types+node@20.17.28/node_modules/@types/node/globals.d.ts:98

stackTraceLimit

static

number

Error.stackTraceLimit;

node_modules/.pnpm/@types+node@20.17.28/node_modules/@types/node/globals.d.ts:100

Methods

captureStackTrace()

static captureStackTrace(targetObject, constructorOpt?): void;

Defined in: node_modules/.pnpm/@types+node@20.17.28/node_modules/@types/node/globals.d.ts:91

Create .stack property on a target object

Parameters

ParameterType

targetObject

object

constructorOpt?

Function

Returns

void

Inherited from

Error.captureStackTrace;