Skip to content

Commit 4c5f2fb

Browse files
authored
feat(core): Allow delayed sending with offline transport (#15937)
This PR adds a extra callback to the offline transport that allows delaying sending of envelopes. This callback has existed in the Electron offline transport for a couple of major versions.
1 parent d007407 commit 4c5f2fb

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/core/src/transports/offline.ts

+15
Original file line numberDiff line numberDiff 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

4556
type 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;

packages/core/test/lib/transports/offline.test.ts

+21
Original file line numberDiff line numberDiff 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());

0 commit comments

Comments
 (0)