UNPKG

@typed/fp

Version:

Data Structures and Resources for fp-ts

58 lines 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.named = exports.criticalExpection = exports.attempt = exports.catchError = exports.catchErrorW = exports.throwError = void 0; const Either_1 = require("fp-ts/Either"); const function_1 = require("fp-ts/function"); const Disposable_1 = require("./Disposable"); const Env_1 = require("./Env"); const Resume_1 = require("./Resume"); const struct_1 = require("./struct"); /** * @since 0.9.2 * @category Constructor */ const throwError = (key) => (err) => (e) => e[key](err); exports.throwError = throwError; const createFailEnv = (key, resume) => (0, struct_1.make)(key, (e) => (0, Resume_1.async)(() => resume(e))); /** * @since 0.9.2 * @category Combinator */ const catchErrorW = (key) => (onError) => (env) => (r) => (0, Resume_1.async)((resume) => (0, function_1.pipe)({ ...r, ...createFailEnv(key, (e) => (0, function_1.pipe)(r, onError(e), (0, Resume_1.run)(resume))) }, env, (0, Resume_1.run)(resume))); exports.catchErrorW = catchErrorW; /** * @since 0.9.2 * @category Combinator */ exports.catchError = exports.catchErrorW; /** * @since 0.9.2 * @category Combinator */ const attempt = (key) => (env) => (0, function_1.pipe)(env, (0, Env_1.map)(Either_1.right), (0, exports.catchErrorW)(key)((e) => (0, Env_1.of)((0, Either_1.left)(e)))); exports.attempt = attempt; /** * Creates a Provider for an Error which will throw an Exception. * Reserve this only for *critical* application errors * @since 0.13.4 * @category Environment */ const criticalExpection = (key) => (f) => createFailEnv(key, (0, Disposable_1.undisposable)((e) => { throw new Error(f(e)); })); exports.criticalExpection = criticalExpection; /** * @since 0.9.2 * @category Constructor */ const named = () => (name) => { return { throw: (0, exports.throwError)(name), catchW: (0, exports.catchErrorW)(name), catch: (0, exports.catchError)(name), attempt: (0, exports.attempt)(name), criticalExpection: (0, exports.criticalExpection)(name), }; }; exports.named = named; //# sourceMappingURL=Fail.js.map