Skip to main content

index

@ts-rust/std


@ts-rust/std / Error

Error

The Error module provides a base error class for handling errors in a consistent way across the @ts-rust/std library. It exports the AnyError class, which serves as a generic error type that can be extended or used directly to represent errors in Option and Result types. Use this module to create or handle custom errors in a type-safe manner within your TypeScript applications.

Classes

ClassDescription

AnyError

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");