7a76476c1be9cdde5a8b6a943dbb7b76feb61a08d6c565261df8784f2a1de8966f59721ee5a7bfe4359e64f2c4b7bc19980498e0327751abd63b8c767a8bff 1.2 KB

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