AnyError
@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 Parameter | Description |
|---|---|
| The type of the error |
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
| Parameter | Type | Description |
|---|---|---|
|
| The descriptive message for the error. |
|
| The category or type of the error, a primitive value. |
|
| An optional underlying cause, which can be any value (converted to |
Returns
AnyError<T>
Overrides
Error.constructor;
Properties
| Property | Modifier | Type | Description | Inherited from | Defined in |
|---|---|---|---|---|---|
|
| ‐ | | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26 | |
|
| 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. | ‐ | ||
|
| ‐ | | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077 | |
|
| ‐ | | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076 | |
|
| The underlying cause of the error, represented as an This readonly property holds the | ‐ | ||
|
| ‐ | | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078 | |
|
| The The default value is If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | | node_modules/.pnpm/@types+node@22.20.1/node_modules/@types/node/globals.d.ts:68 |
Methods
captureStackTrace()
static captureStackTrace(targetObject, constructorOpt?): void;
Defined in: node_modules/.pnpm/@types+node@22.20.1/node_modules/@types/node/globals.d.ts:52
Creates a .stack property on targetObject, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames
above constructorOpt, including constructorOpt, will be omitted from the
generated stack trace.
The constructorOpt argument is useful for hiding implementation
details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
Parameters
| Parameter | Type |
|---|---|
|
|
|
|
Returns
void
Inherited from
Error.captureStackTrace;
prepareStackTrace()
static prepareStackTrace(err, stackTraces): any;
Defined in: node_modules/.pnpm/@types+node@22.20.1/node_modules/@types/node/globals.d.ts:56
Parameters
| Parameter | Type |
|---|---|
|
|
|
|
Returns
any
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from
Error.prepareStackTrace;