libfun
Version:
Make functional programming fun!
19 lines (16 loc) • 408 B
text/typescript
function errorify(what: unknown): Error {
return what instanceof Error
? what
: new Error(typeof what === "string" ? what : JSON.stringify(what));
}
function handle<T = void>(
error: unknown,
catcher?: (error: Error) => void,
fallback?: T
) {
const exception = errorify(error);
if (!catcher) throw exception;
catcher(exception);
return fallback as T;
}
export { errorify, handle };