1
1
import { getCurrentHub , Scope } from '@sentry/core' ;
2
2
import { Integration } from '@sentry/types' ;
3
+ import { consoleSandbox } from '@sentry/utils' ;
4
+
5
+ import { logAndExitProcess } from '../handlers' ;
6
+
7
+ type UnhandledRejectionMode = 'none' | 'warn' | 'strict' ;
3
8
4
9
/** Global Promise Rejection handler */
5
10
export class OnUnhandledRejection implements Integration {
@@ -12,6 +17,19 @@ export class OnUnhandledRejection implements Integration {
12
17
*/
13
18
public static id : string = 'OnUnhandledRejection' ;
14
19
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
+
15
33
/**
16
34
* @inheritDoc
17
35
*/
@@ -28,6 +46,7 @@ export class OnUnhandledRejection implements Integration {
28
46
const hub = getCurrentHub ( ) ;
29
47
30
48
if ( ! hub . getIntegration ( OnUnhandledRejection ) ) {
49
+ this . _handleRejection ( reason ) ;
31
50
return ;
32
51
}
33
52
@@ -49,5 +68,31 @@ export class OnUnhandledRejection implements Integration {
49
68
50
69
hub . captureException ( reason , { originalException : promise } ) ;
51
70
} ) ;
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
+ }
52
97
}
53
98
}
0 commit comments