1 | var _arity =
|
2 | /*#__PURE__*/
|
3 | require("./internal/_arity");
|
4 |
|
5 | var _concat =
|
6 | /*#__PURE__*/
|
7 | require("./internal/_concat");
|
8 |
|
9 | var _curry2 =
|
10 | /*#__PURE__*/
|
11 | require("./internal/_curry2");
|
12 | /**
|
13 | * `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
|
14 | * function evaluates the `tryer`; if it does not throw, it simply returns the
|
15 | * result. If the `tryer` *does* throw, the returned function evaluates the
|
16 | * `catcher` function and returns its result. Note that for effective
|
17 | * composition with this function, both the `tryer` and `catcher` functions
|
18 | * must return the same type of results.
|
19 | *
|
20 | * @func
|
21 | * @memberOf R
|
22 | * @since v0.20.0
|
23 | * @category Function
|
24 | * @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
|
25 | * @param {Function} tryer The function that may throw.
|
26 | * @param {Function} catcher The function that will be evaluated if `tryer` throws.
|
27 | * @return {Function} A new function that will catch exceptions and send then to the catcher.
|
28 | * @example
|
29 | *
|
30 | * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
|
31 | * R.tryCatch(() => { throw 'foo'}, R.always('catched'))('bar') // => 'catched'
|
32 | * R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
|
33 | * R.tryCatch(() => { throw 'this is not a valid value'}, (err, value)=>({error : err, value }))('bar') // => {'error': 'this is not a valid value', 'value': 'bar'}
|
34 | */
|
35 |
|
36 |
|
37 | var tryCatch =
|
38 | /*#__PURE__*/
|
39 | _curry2(function _tryCatch(tryer, catcher) {
|
40 | return _arity(tryer.length, function () {
|
41 | try {
|
42 | return tryer.apply(this, arguments);
|
43 | } catch (e) {
|
44 | return catcher.apply(this, _concat([e], arguments));
|
45 | }
|
46 | });
|
47 | });
|
48 |
|
49 | module.exports = tryCatch; |
\ | No newline at end of file |