123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import { defineIntegration, convertIntegrationFnToClass, getClient, captureException } from '@sentry/core';
- import { consoleSandbox } from '@sentry/utils';
- import { logAndExitProcess } from './utils/errorhandling.js';
- const INTEGRATION_NAME = 'OnUnhandledRejection';
- const _onUnhandledRejectionIntegration = ((options = {}) => {
- const mode = options.mode || 'warn';
- return {
- name: INTEGRATION_NAME,
-
- setupOnce() {},
- setup(client) {
- global.process.on('unhandledRejection', makeUnhandledPromiseHandler(client, { mode }));
- },
- };
- }) ;
- const onUnhandledRejectionIntegration = defineIntegration(_onUnhandledRejectionIntegration);
- const OnUnhandledRejection = convertIntegrationFnToClass(
- INTEGRATION_NAME,
- onUnhandledRejectionIntegration,
- )
- ;
- function makeUnhandledPromiseHandler(
- client,
- options,
- ) {
- return function sendUnhandledPromise(reason, promise) {
- if (getClient() !== client) {
- return;
- }
- captureException(reason, {
- originalException: promise,
- captureContext: {
- extra: { unhandledPromiseRejection: true },
- },
- mechanism: {
- handled: false,
- type: 'onunhandledrejection',
- },
- });
- handleRejection(reason, options);
- };
- }
- function handleRejection(
-
- reason,
- options,
- ) {
-
- const rejectionWarning =
- 'This error originated either by ' +
- 'throwing inside of an async function without a catch block, ' +
- 'or by rejecting a promise which was not handled with .catch().' +
- ' The promise rejected with the reason:';
-
- if (options.mode === 'warn') {
- consoleSandbox(() => {
- console.warn(rejectionWarning);
-
- console.error(reason && reason.stack ? reason.stack : reason);
- });
- } else if (options.mode === 'strict') {
- consoleSandbox(() => {
- console.warn(rejectionWarning);
- });
- logAndExitProcess(reason);
- }
-
- }
- export { OnUnhandledRejection, makeUnhandledPromiseHandler, onUnhandledRejectionIntegration };
|