function from
from(thrown: unknown): Any

Coerces any caught value into a Failure, leaving existing Failures untouched.

Necessary because throw accepts every value in JS — throw 'nope', throw undefined, throw { code: 42 } are all legal and all occur in the wild (DOM callbacks, old libraries, and any Promise.reject(someNonError)). The original is preserved under cause rather than being flattened into a string, so nothing is lost on the way through.

import * as Failure from './failure.ts';

const FileMissing = Failure.define('FileMissing', (d: { path: string }) => `no such file: ${d.path}`);

Failure.from(FileMissing({ path: 'a.ts' })); // the same Failure, untouched
Failure.from(new RangeError('x')); // Failure(Unknown) with the RangeError as `.cause`
Failure.from('nope'); // Failure(Unknown) — even a thrown string survives

Parameters

thrown: unknown

Return Type

Usage

import { from } from "lib/result/failure.ts";