UNPKG

@dillonkearns/elm-graphql

Version:

<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">

52 lines (43 loc) 1.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.EXCEPTION = undefined; exports.error = error; exports.throwException = throwException; exports.catchException = catchException; exports.tryEff = tryEff; var _Eff = require('./Eff'); var _Either = require('./Either'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // This effect is used to annotate code which possibly throws exceptions var EXCEPTION = exports.EXCEPTION = function EXCEPTION() { _classCallCheck(this, EXCEPTION); }; function error(message) { return new Error(message); } // Throw an exception function throwException(e) { return (0, _Eff.inj)(function () { throw e; }); } // Catch an exception by providing an exception handler function catchException(handler, eff) { return (0, _Eff.inj)(function () { try { return (0, _Eff.runEff)(eff); } catch (e) { return (0, _Eff.runEff)(handler(e)); } }); } // Runs an Eff and returns eventual Exceptions as a `Left` value. If the // computation succeeds the result gets wrapped in a `Right`. function tryEff(eff) { var handler = function handler(e) { return (0, _Eff.of)((0, _Either.left)(e)); }; return catchException(handler, (0, _Eff.map)(_Either.right, eff)); }