Passed
Push — master ( 7b6311...b9528f )
by Zhenyu
01:27
created

src/hooks/error-mute.js   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 18
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 5
mnd 1
bc 1
fnc 3
dl 0
loc 18
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) => (condition(e, p, m, c, a) ? e : undefined),
18
  });
19
20
export default errorMute;
21