UNPKG

3.65 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var core_1 = require("@sentry/core");
3var utils_1 = require("@sentry/utils");
4var errorhandling_1 = require("./utils/errorhandling");
5/** Global Promise Rejection handler */
6var OnUnhandledRejection = /** @class */ (function () {
7 /**
8 * @inheritDoc
9 */
10 function OnUnhandledRejection(_options) {
11 if (_options === void 0) { _options = { mode: 'warn' }; }
12 this._options = _options;
13 /**
14 * @inheritDoc
15 */
16 this.name = OnUnhandledRejection.id;
17 }
18 /**
19 * @inheritDoc
20 */
21 OnUnhandledRejection.prototype.setupOnce = function () {
22 global.process.on('unhandledRejection', this.sendUnhandledPromise.bind(this));
23 };
24 /**
25 * Send an exception with reason
26 * @param reason string
27 * @param promise promise
28 */
29 // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
30 OnUnhandledRejection.prototype.sendUnhandledPromise = function (reason, promise) {
31 var hub = core_1.getCurrentHub();
32 if (!hub.getIntegration(OnUnhandledRejection)) {
33 this._handleRejection(reason);
34 return;
35 }
36 /* eslint-disable @typescript-eslint/no-unsafe-member-access */
37 var context = (promise.domain && promise.domain.sentryContext) || {};
38 hub.withScope(function (scope) {
39 scope.setExtra('unhandledPromiseRejection', true);
40 // Preserve backwards compatibility with raven-node for now
41 if (context.user) {
42 scope.setUser(context.user);
43 }
44 if (context.tags) {
45 scope.setTags(context.tags);
46 }
47 if (context.extra) {
48 scope.setExtras(context.extra);
49 }
50 hub.captureException(reason, {
51 originalException: promise,
52 data: { mechanism: { handled: false, type: 'onunhandledrejection' } },
53 });
54 });
55 /* eslint-disable @typescript-eslint/no-unsafe-member-access */
56 this._handleRejection(reason);
57 };
58 /**
59 * Handler for `mode` option
60 */
61 // eslint-disable-next-line @typescript-eslint/no-explicit-any
62 OnUnhandledRejection.prototype._handleRejection = function (reason) {
63 // https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js#L234-L240
64 var rejectionWarning = 'This error originated either by ' +
65 'throwing inside of an async function without a catch block, ' +
66 'or by rejecting a promise which was not handled with .catch().' +
67 ' The promise rejected with the reason:';
68 /* eslint-disable no-console */
69 if (this._options.mode === 'warn') {
70 utils_1.consoleSandbox(function () {
71 console.warn(rejectionWarning);
72 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
73 console.error(reason && reason.stack ? reason.stack : reason);
74 });
75 }
76 else if (this._options.mode === 'strict') {
77 utils_1.consoleSandbox(function () {
78 console.warn(rejectionWarning);
79 });
80 errorhandling_1.logAndExitProcess(reason);
81 }
82 /* eslint-enable no-console */
83 };
84 /**
85 * @inheritDoc
86 */
87 OnUnhandledRejection.id = 'OnUnhandledRejection';
88 return OnUnhandledRejection;
89}());
90exports.OnUnhandledRejection = OnUnhandledRejection;
91//# sourceMappingURL=onunhandledrejection.js.map
\No newline at end of file