File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 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 {
3838 * @param envelope The envelope that failed to send.
3939 * @param error The error that occurred.
4040 * @param retryDelay The current retry delay in milliseconds.
41+ * @returns Whether the envelope should be stored.
4142 */
4243 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 > ;
4354}
4455
4556type Timer = number | { unref ?: ( ) => void } ;
@@ -128,6 +139,10 @@ export function makeOfflineTransport<TO>(
128139 }
129140
130141 try {
142+ if ( options . shouldSend && ( await options . shouldSend ( envelope ) ) === false ) {
143+ throw new Error ( 'Envelope not sent because `shouldSend` callback returned false' ) ;
144+ }
145+
131146 const result = await transport . send ( envelope ) ;
132147
133148 let delay = MIN_DELAY ;
Original file line number Diff line number Diff line change @@ -353,6 +353,27 @@ describe('makeOfflineTransport', () => {
353353 expect ( getCalls ( ) ) . toEqual ( [ ] ) ;
354354 } ) ;
355355
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+
356377 it ( 'should not store client report envelopes on send failure' , async ( ) => {
357378 const { getCalls, store } = createTestStore ( ) ;
358379 const { getSendCount, baseTransport } = createTestTransport ( new Error ( ) ) ;
You can’t perform that action at this time.
0 commit comments