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

src/hooks/error-handler.js   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 24
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 7
mnd 1
bc 1
fnc 4
dl 0
loc 24
rs 10
bpm 0.25
cpm 1.25
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 add conditional side-effect before error being thrown
7
  e.g. It can be used together with error-metrics to create conditional error-metrics
8
  e.g. It can also be used to handle specific error by throw error in the handler.
9
 *
10
 * @template T
11
 * @typedef {import('./types').ErrorHookMethod<T>} ErrorHookMethod
12
 *
13
 * @param {object} options - Config.
14
 * @param {ErrorHookMethod<boolean>} [options.condition] - Condition to call the handler.
15
 * @param {ErrorHookMethod<void>} [options.handler] - What to do when the condition met.
16
 */
17
const errorHandler = ({ condition = () => false, handler = () => {} } = {}) =>
18
  addHooks({
19
    errorHook: (e, p, m, c, a) => {
20
      if (condition(e, p, m, c, a)) {
21
        handler(e, p, m, c, a);
22
      }
23
    },
24
  });
25
26
export default errorHandler;
27