onunhandledrejection.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const errorhandling = require('./utils/errorhandling.js');
  5. const INTEGRATION_NAME = 'OnUnhandledRejection';
  6. const _onUnhandledRejectionIntegration = ((options = {}) => {
  7. const mode = options.mode || 'warn';
  8. return {
  9. name: INTEGRATION_NAME,
  10. // TODO v8: Remove this
  11. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  12. setup(client) {
  13. global.process.on('unhandledRejection', makeUnhandledPromiseHandler(client, { mode }));
  14. },
  15. };
  16. }) ;
  17. const onUnhandledRejectionIntegration = core.defineIntegration(_onUnhandledRejectionIntegration);
  18. /**
  19. * Global Promise Rejection handler.
  20. * @deprecated Use `onUnhandledRejectionIntegration()` instead.
  21. */
  22. // eslint-disable-next-line deprecation/deprecation
  23. const OnUnhandledRejection = core.convertIntegrationFnToClass(
  24. INTEGRATION_NAME,
  25. onUnhandledRejectionIntegration,
  26. )
  27. ;
  28. // eslint-disable-next-line deprecation/deprecation
  29. /**
  30. * Send an exception with reason
  31. * @param reason string
  32. * @param promise promise
  33. *
  34. * Exported only for tests.
  35. */
  36. function makeUnhandledPromiseHandler(
  37. client,
  38. options,
  39. ) {
  40. return function sendUnhandledPromise(reason, promise) {
  41. if (core.getClient() !== client) {
  42. return;
  43. }
  44. core.captureException(reason, {
  45. originalException: promise,
  46. captureContext: {
  47. extra: { unhandledPromiseRejection: true },
  48. },
  49. mechanism: {
  50. handled: false,
  51. type: 'onunhandledrejection',
  52. },
  53. });
  54. handleRejection(reason, options);
  55. };
  56. }
  57. /**
  58. * Handler for `mode` option
  59. */
  60. function handleRejection(
  61. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  62. reason,
  63. options,
  64. ) {
  65. // https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js#L234-L240
  66. const rejectionWarning =
  67. 'This error originated either by ' +
  68. 'throwing inside of an async function without a catch block, ' +
  69. 'or by rejecting a promise which was not handled with .catch().' +
  70. ' The promise rejected with the reason:';
  71. /* eslint-disable no-console */
  72. if (options.mode === 'warn') {
  73. utils.consoleSandbox(() => {
  74. console.warn(rejectionWarning);
  75. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  76. console.error(reason && reason.stack ? reason.stack : reason);
  77. });
  78. } else if (options.mode === 'strict') {
  79. utils.consoleSandbox(() => {
  80. console.warn(rejectionWarning);
  81. });
  82. errorhandling.logAndExitProcess(reason);
  83. }
  84. /* eslint-enable no-console */
  85. }
  86. exports.OnUnhandledRejection = OnUnhandledRejection;
  87. exports.makeUnhandledPromiseHandler = makeUnhandledPromiseHandler;
  88. exports.onUnhandledRejectionIntegration = onUnhandledRejectionIntegration;
  89. //# sourceMappingURL=onunhandledrejection.js.map