(fn: () => T)
| 2 | * optimize try catch |
| 3 | */ |
| 4 | export function tryCatch<T = any>(fn: () => T) { |
| 5 | const res: { |
| 6 | error: Error | undefined, |
| 7 | value: T | undefined, |
| 8 | } = { |
| 9 | error: undefined, |
| 10 | value: undefined, |
| 11 | }; |
| 12 | |
| 13 | try { |
| 14 | res.value = fn(); |
| 15 | } catch (err) { |
| 16 | res.error = err instanceof Error |
| 17 | ? err |
| 18 | : new Error(err as string); |
| 19 | } |
| 20 | |
| 21 | return res; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @description Deal with typescript |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…