Skip to main content

OptionError

@ts-rust/std


@ts-rust/std / Option / OptionError

Interface: OptionError

Defined in: packages/std/src/option/error.ts:52

An error thrown by Option methods when operations fail due to the option's state or unexpected conditions.

This class extends AnyError with error kinds specific to Option operations, as defined in OptionErrorKind. It is typically thrown by methods like unwrap, expect, or others that enforce strict access or behavior on Some or None variants. Use it to handle failures gracefully in a type-safe manner, inspecting the OptionErrorKind to determine the cause.

Example​

const opt = none<number>();
try {
opt.unwrap();
} catch (e) {
expect(e).toBeInstanceOf(OptionError);
expect(e.kind).toBe(OptionErrorKind.UnwrapCalledOnNone);
expect(e.message).toBe("`unwrap`: called on `None`");
}

Extends​

Properties​

PropertyModifierTypeDescriptionInherited fromDefined in

cause?

public

unknown

‐

AnyError.cause

node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es2022.error.d.ts:24

kind

readonly

OptionErrorKind

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.

AnyError.kind

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

message

public

string

‐

AnyError.message

node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1075

name

public

string

‐

AnyError.name

node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1074

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.

AnyError.reason

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

stack?

public

string

‐

AnyError.stack

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