1 | var _arity =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_arity.js");
|
---|
4 |
|
---|
5 | var _concat =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_concat.js");
|
---|
8 |
|
---|
9 | var _curry2 =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./internal/_curry2.js");
|
---|
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 them to the catcher.
|
---|
28 | * @example
|
---|
29 | *
|
---|
30 | * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
|
---|
31 | * R.tryCatch(() => { throw 'foo'}, R.always('caught'))('bar') // =>
|
---|
32 | * 'caught'
|
---|
33 | * R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
|
---|
34 | * R.tryCatch(() => { throw 'this is not a valid value'}, (err, value)=>({error : err, value }))('bar') // => {'error': 'this is not a valid value', 'value': 'bar'}
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | var tryCatch =
|
---|
39 | /*#__PURE__*/
|
---|
40 | _curry2(function _tryCatch(tryer, catcher) {
|
---|
41 | return _arity(tryer.length, function () {
|
---|
42 | try {
|
---|
43 | return tryer.apply(this, arguments);
|
---|
44 | } catch (e) {
|
---|
45 | return catcher.apply(this, _concat([e], arguments));
|
---|
46 | }
|
---|
47 | });
|
---|
48 | });
|
---|
49 |
|
---|
50 | module.exports = tryCatch; |
---|