result.attempt
The value of a raw-tier call as a success, or whatever it throws as a coded failure.
Usage
Result.attempt<T>(run: () => T, code: KernelDiagnosticCode, what: string, details?: KernelDiagnosticDetails): Result<T>Parameters
run() => TThe call to make, typically into the raw
wasm2d/wasm3dtier.codeKernelDiagnosticCodeThe code for an uncoded throw; usually
GEOMETRY_OP_FAILED.whatstringWhat the API was doing, prefixed to the message (
"offset: <kernel message>").detailsKernelDiagnosticDetailsContext added to the failure (
paramPath,measured, …). Default{}.
Returns
Result.ok(run()) when run returns. When it throws: a thrown KernelDiagnosticError becomes a failure with its own diagnostic (its code, not code); anything else (an Error, a plain string) a failure with code.
Guarantees
- it never throws for anything run throws (r.ok === false instead)
- r.ok exactly when run returned
- an uncoded throw keeps the kernel’s own message in measured.kernelMessage (Result.error(r).measured.kernelMessage)
- a thrown KernelDiagnosticError keeps its code (Result.error(r).code === e.diagnostic.code)
Example
const r = Result.attempt(() => { throw 'self-intersecting ring' }, 'GEOMETRY_OP_FAILED', 'offset')
Result.error(r).message // → 'offset: self-intersecting ring'