errgo-ts
Version:
A lightweight error handling library inspired by Go and Rust.
20 lines (19 loc) • 548 B
JavaScript
import { coerceError } from "./coerce-error.js";
export function propagateError(errorContext, action) {
try {
const result = action();
if (result instanceof Promise) {
return result.then((val) => val, (e) => {
const err = coerceError(e);
throw new Error(errorContext, { cause: err });
});
}
else {
return result;
}
}
catch (e) {
const err = coerceError(e);
throw new Error(errorContext, { cause: err });
}
}