Passed
Push — master ( 7b7ee2...57bfe4 )
by Zhenyu
01:17
created

src/hooks/error-mute.js   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 23
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 8
mnd 1
bc 1
fnc 3
dl 0
loc 23
rs 10
bpm 0.3333
cpm 1.3333
noi 0
c 0
b 0
f 0
1
// @ts-check
2
3
import addHooks from './helpers/add-hooks';
4
5
/**
6
 * A decorator to mute errors when conditions are met.
7
 *
8
 * @template T
9
 * @typedef {import('./types').ErrorHookMethod<T>} ErrorHookMethod
10
 *
11
 * @param {object} options - Config.
12
 * @param  {ErrorHookMethod<boolean>} [options.condition] - Condition to mute the error.
13
 * @returns {Error|object|undefined} - If the error is muted(not thrown to upper level) it is accessible in the return value.
14
 */
15
const errorMute = ({ condition = () => true } = {}) =>
16
  addHooks({
17
    errorHook: (e, p, m, c, a) => {
18
      if (condition(e, p, m, c, a)) {
19
        return e;
20
      }
21
      return undefined;
22
    },
23
  });
24
25
export default errorMute;
26