UNPKG

errgo-ts

Version:

A lightweight error handling library inspired by Go and Rust.

20 lines (19 loc) 453 B
import { coerceError } from "./coerce-error.js"; export function safeTry(action) { try { const result = action(); if (result instanceof Promise) { return result.then((v) => ({ val: v, }), (e) => ({ err: coerceError(e), })); } else { return { val: result }; } } catch (e) { return { err: coerceError(e) }; } }