import { panic } from "https://deno.land/x/eitherway@0.10.0/lib/core/errors.ts";
Use this to either throw <E>
directly or wrap it in a Panic<E>
if <E>
is not a subtype of the native Error
type
This can be useful in situations where:
- the encountered error is truly unrecoverable
- error handling is unfeasible
- this behaviour is required per some contract (e.g. middlewares)
- you want to emulate the behavior of
.unwrap()
from other libraries or languages
Examples
Example 1
Example 1
import { Result, Option } from "https://deno.land/x/eitherway/mod.ts";
import { panic } from "./mod.ts"
function parseHex(hex: string): Result<number, TypeError> {
const candidate = Number.parseInt(hex, 16);
return Option.fromCoercible(candidate).okOrElse(() => TypeError());
}
const res = Result("0x10f2c").andThen(parseHex);
const value: number = res.unwrapOrElse(panic);
Parameters
optional
err: E