Skip to main content

UnwrapError

Package: antithrow

The exception thrown when unwrap() is called on an Err or unwrapErr() is called on an Ok. The only class antithrow ever throws.

Signature

class UnwrapError extends Error {
override readonly name: "UnwrapError";
readonly result: Settled<unknown, unknown>;

constructor(message: string, result: Settled<unknown, unknown>);
}

Properties

NameTypeDescription
name"UnwrapError"Always "UnwrapError".
messagestringA short description of which unwrap was called on which shape.
resultSettled<unknown, unknown>The settled result that was being unwrapped.

When thrown

  • new Ok(...).unwrapErr() throws with message "Called unwrapErr() on an Ok value".
  • new Err(...).unwrap() throws with message "Called unwrap() on an Err value".
  • pending.unwrap() or pending.unwrapErr() rejects the returned promise with UnwrapError if the settled state is on the wrong branch.

No other antithrow function throws.

Example

import { Err, UnwrapError } from "antithrow";

const result = new Err<number, string>("failed");

try {
result.unwrap();
} catch (error) {
if (error instanceof UnwrapError) {
error.result.isErr(); // true
error.result.isErr() && error.result.error; // "failed"
}
}

See also