| 1 |
import { config } from '../config'; |
| 2 |
|
| 3 |
let context: { errorThrown: boolean; error: any } | null = null; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles dealing with errors for super-gross mode. Creates a context, in which |
| 7 |
* any synchronously thrown errors will be passed to {@link captureError}. Which |
| 8 |
* will record the error such that it will be rethrown after the call back is complete. |
| 9 |
* TODO: Remove in v8 |
| 10 |
* @param cb An immediately executed function. |
| 11 |
*/ |
| 12 |
export function errorContext(cb: () => void) { |
| 13 |
if (config.useDeprecatedSynchronousErrorHandling) { |
| 14 |
const isRoot = !context; |
| 15 |
if (isRoot) { |
| 16 |
context = { errorThrown: false, error: null }; |
| 17 |
} |
| 18 |
cb(); |
| 19 |
if (isRoot) { |
| 20 |
const { errorThrown, error } = context!; |
| 21 |
context = null; |
| 22 |
if (errorThrown) { |
| 23 |
throw error; |
| 24 |
} |
| 25 |
} |
| 26 |
} else { |
| 27 |
// This is the general non-deprecated path for everyone that |
| 28 |
// isn't crazy enough to use super-gross mode (useDeprecatedSynchronousErrorHandling) |
| 29 |
cb(); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Captures errors only in super-gross mode. |
| 35 |
* @param err the error to capture |
| 36 |
*/ |
| 37 |
export function captureError(err: any) { |
| 38 |
if (config.useDeprecatedSynchronousErrorHandling && context) { |
| 39 |
context.errorThrown = true; |
| 40 |
context.error = err; |
| 41 |
} |
| 42 |
} |