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
| Name | Type | Description |
|---|---|---|
name | "UnwrapError" | Always "UnwrapError". |
message | string | A short description of which unwrap was called on which shape. |
result | Settled<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()orpending.unwrapErr()rejects the returned promise withUnwrapErrorif 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
Resultchainable methods —unwrap,unwrapErr,unwrapOr,unwrapOrElse.- Typed errors and the role of UnwrapError — the rationale for throwing from
unwrap.