@@ -135,10 +135,12 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
135
135
136
136
this . getBackend ( )
137
137
. eventFromException ( exception , hint )
138
- . then ( event => {
139
- this . processEvent ( event , hint , scope ) . then ( finalEvent => {
140
- eventId = ( finalEvent && finalEvent . event_id ) || undefined ;
141
- } ) ;
138
+ . then ( event => this . processEvent ( event , hint , scope ) )
139
+ . then ( finalEvent => {
140
+ eventId = finalEvent . event_id ;
141
+ } )
142
+ . catch ( reason => {
143
+ logger . log ( reason ) ;
142
144
} ) ;
143
145
144
146
return eventId ;
@@ -154,11 +156,14 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
154
156
? this . getBackend ( ) . eventFromMessage ( `${ message } ` , level , hint )
155
157
: this . getBackend ( ) . eventFromException ( message , hint ) ;
156
158
157
- promisedEvent . then ( event => {
158
- this . processEvent ( event , hint , scope ) . then ( finalEvent => {
159
- eventId = ( finalEvent && finalEvent . event_id ) || undefined ;
159
+ promisedEvent
160
+ . then ( event => this . processEvent ( event , hint , scope ) )
161
+ . then ( finalEvent => {
162
+ eventId = finalEvent . event_id ;
163
+ } )
164
+ . catch ( reason => {
165
+ logger . log ( reason ) ;
160
166
} ) ;
161
- } ) ;
162
167
163
168
return eventId ;
164
169
}
@@ -168,9 +173,13 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
168
173
*/
169
174
public captureEvent ( event : SentryEvent , hint ?: SentryEventHint , scope ?: Scope ) : string | undefined {
170
175
let eventId : string | undefined = hint && hint . event_id ;
171
- this . processEvent ( event , hint , scope ) . then ( finalEvent => {
172
- eventId = ( finalEvent && finalEvent . event_id ) || undefined ;
173
- } ) ;
176
+ this . processEvent ( event , hint , scope )
177
+ . then ( finalEvent => {
178
+ eventId = finalEvent . event_id ;
179
+ } )
180
+ . catch ( reason => {
181
+ logger . log ( reason ) ;
182
+ } ) ;
174
183
return eventId ;
175
184
}
176
185
@@ -270,13 +279,17 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
270
279
prepared . event_id = uuid4 ( ) ;
271
280
}
272
281
282
+ // We prepare the result here with a resolved SentryEvent.
283
+ let result = SyncPromise . resolve < SentryEvent | null > ( prepared ) ;
284
+
273
285
// This should be the last thing called, since we want that
274
286
// {@link Hub.addEventProcessor } gets the finished prepared event.
275
287
if ( scope ) {
276
- return scope . applyToEvent ( prepared , hint , Math . min ( maxBreadcrumbs , MAX_BREADCRUMBS ) ) ;
288
+ // In case we have a hub we reassign it.
289
+ result = scope . applyToEvent ( prepared , hint , Math . min ( maxBreadcrumbs , MAX_BREADCRUMBS ) ) ;
277
290
}
278
291
279
- return SyncPromise . resolve ( prepared as SentryEvent | null ) ;
292
+ return result ;
280
293
}
281
294
282
295
/**
@@ -286,80 +299,57 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
286
299
* platform specific meta data (such as the User's IP address) must be added
287
300
* by the SDK implementor.
288
301
*
289
- * The returned event status offers clues to whether the event was sent to
290
- * Sentry and accepted there. If the {@link Options.shouldSend} hook returns
291
- * `false`, the status will be {@link SendStatus.Skipped}. If the rate limit
292
- * was exceeded, the status will be {@link SendStatus.RateLimit}.
293
302
*
294
303
* @param event The event to send to Sentry.
295
- * @param send A function to actually send the event.
296
- * @param scope A scope containing event metadata.
297
304
* @param hint May contain additional informartion about the original exception.
298
- * @returns A SyncPromise that resolves with the event.
305
+ * @param scope A scope containing event metadata.
306
+ * @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send.
299
307
*/
300
- protected processEvent ( event : SentryEvent , hint ?: SentryEventHint , scope ?: Scope ) : SyncPromise < SentryEvent | null > {
308
+ protected processEvent ( event : SentryEvent , hint ?: SentryEventHint , scope ?: Scope ) : SyncPromise < SentryEvent > {
301
309
const { beforeSend, sampleRate } = this . getOptions ( ) ;
302
310
303
311
if ( ! this . isEnabled ( ) ) {
304
- logger . log ( 'SDK not enabled, will not send event.' ) ;
305
- return SyncPromise . resolve ( ) ;
312
+ return SyncPromise . reject ( 'SDK not enabled, will not send event.' ) ;
306
313
}
307
314
308
315
// 1.0 === 100% events are sent
309
316
// 0.0 === 0% events are sent
310
317
if ( typeof sampleRate === 'number' && Math . random ( ) > sampleRate ) {
311
- logger . log ( 'This event has been sampled, will not send event.' ) ;
312
- return SyncPromise . resolve ( ) ;
318
+ return SyncPromise . reject ( 'This event has been sampled, will not send event.' ) ;
313
319
}
314
320
315
- return new SyncPromise ( resolve => {
321
+ return new SyncPromise ( ( resolve , reject ) => {
316
322
this . prepareEvent ( event , scope , hint ) . then ( prepared => {
317
323
if ( prepared === null ) {
318
- logger . log ( 'An event processor returned null, will not send event.' ) ;
319
- resolve ( null ) ;
324
+ reject ( 'An event processor returned null, will not send event.' ) ;
320
325
return ;
321
326
}
322
327
323
328
let finalEvent : SentryEvent | null = prepared ;
324
329
325
330
try {
326
331
const isInternalException = hint && hint . data && ( hint . data as { [ key : string ] : any } ) . __sentry__ === true ;
327
- if ( ! isInternalException && beforeSend ) {
328
- const beforeSendResult = beforeSend ( prepared , hint ) ;
329
- if ( ( typeof beforeSendResult as any ) === 'undefined' ) {
330
- logger . error ( '`beforeSend` method has to return `null` or a valid event.' ) ;
331
- } else if ( isThenable ( beforeSendResult ) ) {
332
- ( beforeSendResult as Promise < SentryEvent | null > )
333
- . then ( processedEvent => {
334
- finalEvent = processedEvent ;
335
-
336
- if ( finalEvent === null ) {
337
- logger . log ( '`beforeSend` returned `null`, will not send event.' ) ;
338
- resolve ( null ) ;
339
- return ;
340
- }
341
-
342
- // From here on we are really async
343
- this . getBackend ( ) . sendEvent ( finalEvent ) ;
344
- resolve ( finalEvent ) ;
345
- } )
346
- . catch ( e => {
347
- logger . error ( `beforeSend rejected with ${ e } ` ) ;
348
- } ) ;
349
- } else {
350
- finalEvent = beforeSendResult as SentryEvent | null ;
351
-
352
- if ( finalEvent === null ) {
353
- logger . log ( '`beforeSend` returned `null`, will not send event.' ) ;
354
- resolve ( null ) ;
355
- return ;
356
- }
357
-
358
- // From here on we are really async
359
- this . getBackend ( ) . sendEvent ( finalEvent ) ;
360
- resolve ( finalEvent ) ;
361
- }
332
+ if ( isInternalException || ! beforeSend ) {
333
+ this . getBackend ( ) . sendEvent ( finalEvent ) ;
334
+ resolve ( finalEvent ) ;
335
+ return ;
336
+ }
337
+
338
+ const beforeSendResult = beforeSend ( prepared , hint ) ;
339
+ if ( ( typeof beforeSendResult as any ) === 'undefined' ) {
340
+ logger . error ( '`beforeSend` method has to return `null` or a valid event.' ) ;
341
+ } else if ( isThenable ( beforeSendResult ) ) {
342
+ this . handleAsyncBeforeSend ( beforeSendResult as Promise < SentryEvent | null > , resolve , reject ) ;
362
343
} else {
344
+ finalEvent = beforeSendResult as SentryEvent | null ;
345
+
346
+ if ( finalEvent === null ) {
347
+ logger . log ( '`beforeSend` returned `null`, will not send event.' ) ;
348
+ resolve ( null ) ;
349
+ return ;
350
+ }
351
+
352
+ // From here on we are really async
363
353
this . getBackend ( ) . sendEvent ( finalEvent ) ;
364
354
resolve ( finalEvent ) ;
365
355
}
@@ -370,14 +360,35 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
370
360
} ,
371
361
originalException : exception as Error ,
372
362
} ) ;
373
- logger . error ( '`beforeSend` throw an error, will not send event.' ) ;
374
- resolve ( null ) ;
375
- return ;
363
+ reject ( '`beforeSend` throw an error, will not send event.' ) ;
376
364
}
377
365
} ) ;
378
366
} ) ;
379
367
}
380
368
369
+ /**
370
+ * Resolves before send Promise and calls resolve/reject on parent SyncPromise.
371
+ */
372
+ private handleAsyncBeforeSend (
373
+ beforeSend : Promise < SentryEvent | null > ,
374
+ resolve : ( event : SentryEvent ) => void ,
375
+ reject : ( reason : string ) => void ,
376
+ ) : void {
377
+ beforeSend
378
+ . then ( processedEvent => {
379
+ if ( processedEvent === null ) {
380
+ reject ( '`beforeSend` returned `null`, will not send event.' ) ;
381
+ return ;
382
+ }
383
+ // From here on we are really async
384
+ this . getBackend ( ) . sendEvent ( processedEvent ) ;
385
+ resolve ( processedEvent ) ;
386
+ } )
387
+ . catch ( e => {
388
+ reject ( `beforeSend rejected with ${ e } ` ) ;
389
+ } ) ;
390
+ }
391
+
381
392
/**
382
393
* @inheritDoc
383
394
*/
0 commit comments