this repo has no description
1/**
2 * Tries to call {@linkcode fn} throwing an exception with the {@linkcode message}
3 * if an error occurs
4 *
5 * @example
6 * // Before
7 * let value;
8 * try {
9 * value = someMethod();
10 * } catch(e) {
11 * throw new Error('My specific message', { cause: e })
12 * }
13 *
14 * // After
15 * const value = mapError(
16 * () => someMethod(),
17 * 'My specific message'
18 * );
19 */
20export function mapException<T>(fn: () => T, message: string): T {
21 try {
22 return fn();
23 } catch (e) {
24 throw new Error(message, {
25 cause: e,
26 });
27 }
28}