result.attempt

The value of a raw-tier call as a success, or whatever it throws as a coded failure.

GeometricSince 0.3.0

Usage

TypeScript
Result.attempt<T>(run: () => T, code: KernelDiagnosticCode, what: string, details?: KernelDiagnosticDetails): Result<T>

Parameters

  • run() => T

    The call to make, typically into the raw wasm2d / wasm3d tier.

    Required

  • codeKernelDiagnosticCode

    The code for an uncoded throw; usually GEOMETRY_OP_FAILED.

    Required

  • whatstring

    What the API was doing, prefixed to the message ("offset: <kernel message>").

    Required

  • detailsKernelDiagnosticDetails

    Context 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

TypeScript
const r = Result.attempt(() => { throw 'self-intersecting ring' }, 'GEOMETRY_OP_FAILED', 'offset')
Result.error(r).message // → 'offset: self-intersecting ring'

See also