File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -38,8 +38,19 @@ export interface OfflineTransportOptions extends InternalBaseTransportOptions {
38
38
* @param envelope The envelope that failed to send.
39
39
* @param error The error that occurred.
40
40
* @param retryDelay The current retry delay in milliseconds.
41
+ * @returns Whether the envelope should be stored.
41
42
*/
42
43
shouldStore ?: ( envelope : Envelope , error : Error , retryDelay : number ) => boolean | Promise < boolean > ;
44
+
45
+ /**
46
+ * Should an attempt be made to send the envelope to Sentry.
47
+ *
48
+ * If this function is supplied and returns false, `shouldStore` will be called to determine if the envelope should be stored.
49
+ *
50
+ * @param envelope The envelope that will be sent.
51
+ * @returns Whether we should attempt to send the envelope
52
+ */
53
+ shouldSend ?: ( envelope : Envelope ) => boolean | Promise < boolean > ;
43
54
}
44
55
45
56
type Timer = number | { unref ?: ( ) => void } ;
@@ -128,6 +139,10 @@ export function makeOfflineTransport<TO>(
128
139
}
129
140
130
141
try {
142
+ if ( options . shouldSend && ( await options . shouldSend ( envelope ) ) === false ) {
143
+ throw new Error ( 'Envelope not sent because `shouldSend` callback returned false' ) ;
144
+ }
145
+
131
146
const result = await transport . send ( envelope ) ;
132
147
133
148
let delay = MIN_DELAY ;
Original file line number Diff line number Diff line change @@ -353,6 +353,27 @@ describe('makeOfflineTransport', () => {
353
353
expect ( getCalls ( ) ) . toEqual ( [ ] ) ;
354
354
} ) ;
355
355
356
+ it ( 'shouldSend can stop envelopes from being sent' , async ( ) => {
357
+ const { getCalls, store } = createTestStore ( ) ;
358
+ const { getSendCount, baseTransport } = createTestTransport ( new Error ( ) ) ;
359
+ let queuedCount = 0 ;
360
+ const transport = makeOfflineTransport ( baseTransport ) ( {
361
+ ...transportOptions ,
362
+ createStore : store ,
363
+ shouldSend : ( ) => false ,
364
+ shouldStore : ( ) => {
365
+ queuedCount += 1 ;
366
+ return true ;
367
+ } ,
368
+ } ) ;
369
+ const result = transport . send ( ERROR_ENVELOPE ) ;
370
+
371
+ await expect ( result ) . resolves . toEqual ( { } ) ;
372
+ expect ( queuedCount ) . toEqual ( 1 ) ;
373
+ expect ( getSendCount ( ) ) . toEqual ( 0 ) ;
374
+ expect ( getCalls ( ) ) . toEqual ( [ 'push' ] ) ;
375
+ } ) ;
376
+
356
377
it ( 'should not store client report envelopes on send failure' , async ( ) => {
357
378
const { getCalls, store } = createTestStore ( ) ;
358
379
const { getSendCount, baseTransport } = createTestTransport ( new Error ( ) ) ;
You can’t perform that action at this time.
0 commit comments