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