Skip to content

Commit d716bd9

Browse files
kamilogorekHazAT
authored andcommittedNov 13, 2019
feat: Introduce mode option for global promise handler in node (#2312)
1 parent 9e68d29 commit d716bd9

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed
 

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
- Coming soon...
66

7+
## 5.9.0
8+
9+
- [node] feat: Added `mode` option for `OnUnhandledRejection` integration that changes how we log errors and what we do with the process itself
10+
- [browser] ref: Both global handlers now always return `true` to call default implementations (error logging)
11+
712
## 5.8.0
813

914
- [browser/node] feat: 429 http code handling in node/browser transports (#2300)

‎packages/browser/src/integrations/globalhandlers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class GlobalHandlers implements Integration {
9292
if (self._oldOnErrorHandler) {
9393
return self._oldOnErrorHandler.apply(this, arguments);
9494
}
95-
return false;
95+
return true;
9696
}
9797

9898
const client = currentHub.getClient();
@@ -121,7 +121,7 @@ export class GlobalHandlers implements Integration {
121121
return self._oldOnErrorHandler.apply(this, arguments);
122122
}
123123

124-
return false;
124+
return true;
125125
};
126126

127127
this._onErrorHandlerInstalled = true;
@@ -152,7 +152,7 @@ export class GlobalHandlers implements Integration {
152152
if (self._oldOnUnhandledRejectionHandler) {
153153
return self._oldOnUnhandledRejectionHandler.apply(this, arguments);
154154
}
155-
return false;
155+
return true;
156156
}
157157

158158
const client = currentHub.getClient();
@@ -178,7 +178,7 @@ export class GlobalHandlers implements Integration {
178178
return self._oldOnUnhandledRejectionHandler.apply(this, arguments);
179179
}
180180

181-
return false;
181+
return true;
182182
};
183183

184184
this._onUnhandledRejectionHandlerInstalled = true;

‎packages/node/src/handlers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,9 @@ export function errorHandler(options?: {
348348
/**
349349
* @hidden
350350
*/
351-
export function defaultOnFatalError(error: Error): void {
351+
export function logAndExitProcess(error: Error): void {
352352
console.error(error && error.stack ? error.stack : error);
353+
353354
const client = getCurrentHub().getClient<NodeClient>();
354355

355356
if (client === undefined) {

‎packages/node/src/integrations/onuncaughtexception.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Integration, Severity } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44

55
import { NodeClient } from '../client';
6-
import { defaultOnFatalError } from '../handlers';
6+
import { logAndExitProcess } from '../handlers';
77

88
/** Global Promise Rejection handler */
99
export class OnUncaughtException implements Integration {
@@ -54,7 +54,7 @@ export class OnUncaughtException implements Integration {
5454
return (error: Error): void => {
5555
type onFatalErrorHandlerType = (firstError: Error, secondError?: Error) => void;
5656

57-
let onFatalError: onFatalErrorHandlerType = defaultOnFatalError;
57+
let onFatalError: onFatalErrorHandlerType = logAndExitProcess;
5858
const client = getCurrentHub().getClient<NodeClient>();
5959

6060
if (this._options.onFatalError) {
@@ -90,7 +90,7 @@ export class OnUncaughtException implements Integration {
9090
} else if (calledFatalError) {
9191
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
9292
logger.warn('uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown');
93-
defaultOnFatalError(error);
93+
logAndExitProcess(error);
9494
} else if (!caughtSecondError) {
9595
// two cases for how we can hit this branch:
9696
// - capturing of first error blew up and we just caught the exception from that

‎packages/node/src/integrations/onunhandledrejection.ts

+45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { getCurrentHub, Scope } from '@sentry/core';
22
import { Integration } from '@sentry/types';
3+
import { consoleSandbox } from '@sentry/utils';
4+
5+
import { logAndExitProcess } from '../handlers';
6+
7+
type UnhandledRejectionMode = 'none' | 'warn' | 'strict';
38

49
/** Global Promise Rejection handler */
510
export class OnUnhandledRejection implements Integration {
@@ -12,6 +17,19 @@ export class OnUnhandledRejection implements Integration {
1217
*/
1318
public static id: string = 'OnUnhandledRejection';
1419

20+
/**
21+
* @inheritDoc
22+
*/
23+
public constructor(
24+
private readonly _options: {
25+
/**
26+
* Option deciding what to do after capturing unhandledRejection,
27+
* that mimicks behavior of node's --unhandled-rejection flag.
28+
*/
29+
mode: UnhandledRejectionMode;
30+
} = { mode: 'warn' },
31+
) {}
32+
1533
/**
1634
* @inheritDoc
1735
*/
@@ -28,6 +46,7 @@ export class OnUnhandledRejection implements Integration {
2846
const hub = getCurrentHub();
2947

3048
if (!hub.getIntegration(OnUnhandledRejection)) {
49+
this._handleRejection(reason);
3150
return;
3251
}
3352

@@ -49,5 +68,31 @@ export class OnUnhandledRejection implements Integration {
4968

5069
hub.captureException(reason, { originalException: promise });
5170
});
71+
72+
this._handleRejection(reason);
73+
}
74+
75+
/**
76+
* Handler for `mode` option
77+
*/
78+
private _handleRejection(reason: any): void {
79+
// https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js#L234-L240
80+
const rejectionWarning =
81+
'This error originated either by ' +
82+
'throwing inside of an async function without a catch block, ' +
83+
'or by rejecting a promise which was not handled with .catch().' +
84+
' The promise rejected with the reason:';
85+
86+
if (this._options.mode === 'warn') {
87+
consoleSandbox(() => {
88+
console.warn(rejectionWarning);
89+
console.error(reason && reason.stack ? reason.stack : reason);
90+
});
91+
} else if (this._options.mode === 'strict') {
92+
consoleSandbox(() => {
93+
console.warn(rejectionWarning);
94+
});
95+
logAndExitProcess(reason);
96+
}
5297
}
5398
}

0 commit comments

Comments
 (0)