Total Complexity | 5 |
Complexity/F | 1.25 |
Lines of Code | 24 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // @ts-check |
||
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 |