globalErrorHandlers.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.restoreGlobalErrorHandlers = exports.injectGlobalErrorHandlers = void 0;
  6. var _state = require('./state');
  7. /**
  8. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. */
  13. const uncaught = error => {
  14. (0, _state.dispatchSync)({
  15. error,
  16. name: 'error'
  17. });
  18. };
  19. const injectGlobalErrorHandlers = parentProcess => {
  20. const uncaughtException = process.listeners('uncaughtException').slice();
  21. const unhandledRejection = process.listeners('unhandledRejection').slice();
  22. parentProcess.removeAllListeners('uncaughtException');
  23. parentProcess.removeAllListeners('unhandledRejection');
  24. parentProcess.on('uncaughtException', uncaught);
  25. parentProcess.on('unhandledRejection', uncaught);
  26. return {
  27. uncaughtException,
  28. unhandledRejection
  29. };
  30. };
  31. exports.injectGlobalErrorHandlers = injectGlobalErrorHandlers;
  32. const restoreGlobalErrorHandlers = (parentProcess, originalErrorHandlers) => {
  33. parentProcess.removeListener('uncaughtException', uncaught);
  34. parentProcess.removeListener('unhandledRejection', uncaught);
  35. for (const listener of originalErrorHandlers.uncaughtException) {
  36. parentProcess.on('uncaughtException', listener);
  37. }
  38. for (const listener of originalErrorHandlers.unhandledRejection) {
  39. parentProcess.on('unhandledRejection', listener);
  40. }
  41. };
  42. exports.restoreGlobalErrorHandlers = restoreGlobalErrorHandlers;