File tree 8 files changed +50
-20
lines changed
8 files changed +50
-20
lines changed Original file line number Diff line number Diff line change @@ -87,7 +87,7 @@ export class Breadcrumbs implements Integration {
87
87
// https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API/Using_the_Beacon_API
88
88
const result = originalBeaconFunction . apply ( this , args ) ;
89
89
90
- const client = getCurrentHub ( ) . getClient ( ) as BrowserClient ;
90
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
91
91
const dsn = client && client . getDsn ( ) ;
92
92
if ( dsn ) {
93
93
const filterUrl = new API ( dsn ) . getStoreEndpoint ( ) ;
@@ -205,7 +205,7 @@ export class Breadcrumbs implements Integration {
205
205
method = args [ 1 ] . method ;
206
206
}
207
207
208
- const client = getCurrentHub ( ) . getClient ( ) as BrowserClient ;
208
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
209
209
const dsn = client && client . getDsn ( ) ;
210
210
if ( dsn ) {
211
211
const filterUrl = new API ( dsn ) . getStoreEndpoint ( ) ;
@@ -376,7 +376,7 @@ export class Breadcrumbs implements Integration {
376
376
url : args [ 1 ] ,
377
377
} ;
378
378
379
- const client = getCurrentHub ( ) . getClient ( ) as BrowserClient ;
379
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
380
380
const dsn = client && client . getDsn ( ) ;
381
381
if ( dsn ) {
382
382
const filterUrl = new API ( dsn ) . getStoreEndpoint ( ) ;
Original file line number Diff line number Diff line change @@ -87,7 +87,10 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
87
87
if ( ! options . eventId ) {
88
88
options . eventId = getCurrentHub ( ) . lastEventId ( ) ;
89
89
}
90
- ( getCurrentHub ( ) . getClient ( ) as BrowserClient ) . showReportDialog ( options ) ;
90
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
91
+ if ( client ) {
92
+ client . showReportDialog ( options ) ;
93
+ }
91
94
}
92
95
93
96
/**
@@ -122,7 +125,11 @@ export function onLoad(callback: () => void): void {
122
125
* @param timeout Maximum time in ms the client should wait.
123
126
*/
124
127
export async function flush ( timeout ?: number ) : Promise < boolean > {
125
- return ( getCurrentHub ( ) . getClient ( ) as BrowserClient ) . flush ( timeout ) ;
128
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
129
+ if ( client ) {
130
+ return client . flush ( timeout ) ;
131
+ }
132
+ return Promise . reject ( false ) ;
126
133
}
127
134
128
135
/**
@@ -132,5 +139,9 @@ export async function flush(timeout?: number): Promise<boolean> {
132
139
* @param timeout Maximum time in ms the client should wait.
133
140
*/
134
141
export async function close ( timeout ?: number ) : Promise < boolean > {
135
- return ( getCurrentHub ( ) . getClient ( ) as BrowserClient ) . close ( timeout ) ;
142
+ const client = getCurrentHub ( ) . getClient < BrowserClient > ( ) ;
143
+ if ( client ) {
144
+ return client . close ( timeout ) ;
145
+ }
146
+ return Promise . reject ( false ) ;
136
147
}
Original file line number Diff line number Diff line change 1
1
import { addGlobalEventProcessor , getCurrentHub } from '@sentry/hub' ;
2
- import { Client , Event , Integration } from '@sentry/types' ;
2
+ import { Event , Integration } from '@sentry/types' ;
3
3
import { isRegExp } from '@sentry/utils/is' ;
4
4
import { logger } from '@sentry/utils/logger' ;
5
5
import { getEventDescription } from '@sentry/utils/misc' ;
@@ -40,7 +40,7 @@ export class InboundFilters implements Integration {
40
40
}
41
41
const self = hub . getIntegration ( InboundFilters ) ;
42
42
if ( self ) {
43
- const client = hub . getClient ( ) as Client ;
43
+ const client = hub . getClient ( ) ;
44
44
const clientOptions = client ? client . getOptions ( ) : { } ;
45
45
const options = self . _mergeOptions ( clientOptions ) ;
46
46
if ( self . _shouldDropEvent ( event , options ) ) {
Original file line number Diff line number Diff line change @@ -134,8 +134,8 @@ export class Hub implements HubInterface {
134
134
/**
135
135
* @inheritDoc
136
136
*/
137
- public getClient ( ) : Client | undefined {
138
- return this . getStackTop ( ) . client ;
137
+ public getClient < C extends Client > ( ) : C | undefined {
138
+ return this . getStackTop ( ) . client as C ;
139
139
}
140
140
141
141
/** Returns the scope of the top stack. */
Original file line number Diff line number Diff line change @@ -291,12 +291,20 @@ export function errorHandler(): (
291
291
*/
292
292
export function defaultOnFatalError ( error : Error ) : void {
293
293
console . error ( error && error . stack ? error . stack : error ) ;
294
- const options = ( getCurrentHub ( ) . getClient ( ) as NodeClient ) . getOptions ( ) ;
294
+ const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ;
295
+
296
+ if ( client === undefined ) {
297
+ logger . warn ( 'No NodeClient was defined, we are exiting the process now.' ) ;
298
+ global . process . exit ( 1 ) ;
299
+ return ;
300
+ }
301
+
302
+ const options = client . getOptions ( ) ;
295
303
const timeout =
296
304
( options && options . shutdownTimeout && options . shutdownTimeout > 0 && options . shutdownTimeout ) ||
297
305
DEFAULT_SHUTDOWN_TIMEOUT ;
298
306
forget (
299
- ( getCurrentHub ( ) . getClient ( ) as NodeClient ) . close ( timeout ) . then ( ( result : boolean ) => {
307
+ client . close ( timeout ) . then ( ( result : boolean ) => {
300
308
if ( ! result ) {
301
309
logger . warn ( 'We reached the timeout for emptying the request buffer, still exiting now!' ) ;
302
310
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { Integration, Severity } from '@sentry/types';
3
3
import { logger } from '@sentry/utils/logger' ;
4
4
5
5
import { NodeOptions } from '../backend' ;
6
+ import { NodeClient } from '../client' ;
6
7
import { defaultOnFatalError } from '../handlers' ;
7
8
8
9
/** Global Promise Rejection handler */
@@ -55,12 +56,12 @@ export class OnUncaughtException implements Integration {
55
56
type onFatalErrorHandlerType = ( firstError : Error , secondError ?: Error ) => void ;
56
57
57
58
let onFatalError : onFatalErrorHandlerType = defaultOnFatalError ;
58
- const client = getCurrentHub ( ) . getClient ( ) ;
59
+ const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ;
59
60
60
61
if ( this . _options . onFatalError ) {
61
62
onFatalError = this . _options . onFatalError ;
62
- } else if ( client && ( client . getOptions ( ) as NodeOptions ) . onFatalError ) {
63
- onFatalError = ( client . getOptions ( ) as NodeOptions ) . onFatalError as onFatalErrorHandlerType ;
63
+ } else if ( client && client . getOptions ( ) . onFatalError ) {
64
+ onFatalError = client . getOptions ( ) . onFatalError as onFatalErrorHandlerType ;
64
65
}
65
66
66
67
if ( ! caughtFirstError ) {
Original file line number Diff line number Diff line change @@ -115,7 +115,11 @@ export function lastEventId(): string | undefined {
115
115
* @param timeout Maximum time in ms the client should wait.
116
116
*/
117
117
export async function flush ( timeout ?: number ) : Promise < boolean > {
118
- return ( getCurrentHub ( ) . getClient ( ) as NodeClient ) . flush ( timeout ) ;
118
+ const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ;
119
+ if ( client ) {
120
+ return client . flush ( timeout ) ;
121
+ }
122
+ return Promise . reject ( false ) ;
119
123
}
120
124
121
125
/**
@@ -125,5 +129,9 @@ export async function flush(timeout?: number): Promise<boolean> {
125
129
* @param timeout Maximum time in ms the client should wait.
126
130
*/
127
131
export async function close ( timeout ?: number ) : Promise < boolean > {
128
- return ( getCurrentHub ( ) . getClient ( ) as NodeClient ) . close ( timeout ) ;
132
+ const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ;
133
+ if ( client ) {
134
+ return client . close ( timeout ) ;
135
+ }
136
+ return Promise . reject ( false ) ;
129
137
}
Original file line number Diff line number Diff line change @@ -23,14 +23,16 @@ describe('unhandled promises', () => {
23
23
const captureException = jest . spyOn ( Hub . prototype , 'captureException' ) ;
24
24
const setUser = jest . spyOn ( Scope . prototype , 'setUser' ) ;
25
25
const setExtra = jest . spyOn ( Scope . prototype , 'setExtra' ) ;
26
- const setTag = jest . spyOn ( Scope . prototype , 'setTag' ) ;
26
+ const setExtras = jest . spyOn ( Scope . prototype , 'setExtras' ) ;
27
+ const setTags = jest . spyOn ( Scope . prototype , 'setTags' ) ;
27
28
28
29
integration . sendUnhandledPromise ( 'bla' , promise ) ;
29
30
30
31
expect ( captureException . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'bla' ) ;
31
32
expect ( setUser . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { id : 1 } ) ;
32
33
expect ( setExtra . mock . calls [ 0 ] ) . toEqual ( [ 'unhandledPromiseRejection' , true ] ) ;
33
- expect ( setExtra . mock . calls [ 1 ] ) . toEqual ( [ 'extra' , '1' ] ) ;
34
- expect ( setTag . mock . calls [ 0 ] ) . toEqual ( [ 'tag' , '2' ] ) ;
34
+
35
+ expect ( setExtras . mock . calls [ 0 ] ) . toEqual ( [ { extra : '1' } ] ) ;
36
+ expect ( setTags . mock . calls [ 0 ] ) . toEqual ( [ { tag : '2' } ] ) ;
35
37
} ) ;
36
38
} ) ;
You can’t perform that action at this time.
0 commit comments