Skip to content

Commit 67a0dcf

Browse files
authored
feat(replay): Attach an error cause to send exceptions (#7350)
Attach the original exception as a `cause` when we throw "Unable to send replay" exceptions so that we know what is causing issues with sending replays.
1 parent 486d4be commit 67a0dcf

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

packages/replay/src/util/sendReplay.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@ export async function sendReplay(
4141
// If an error happened here, it's likely that uploading the attachment
4242
// failed, we'll can retry with the same events payload
4343
if (retryConfig.count >= RETRY_MAX_COUNT) {
44-
throw new Error(`${UNABLE_TO_SEND_REPLAY} - max retries exceeded`);
44+
const error = new Error(`${UNABLE_TO_SEND_REPLAY} - max retries exceeded`);
45+
46+
try {
47+
// In case browsers don't allow this property to be writable
48+
// @ts-ignore This needs lib es2022 and newer
49+
error.cause = err;
50+
} catch {
51+
// nothing to do
52+
}
53+
54+
throw error;
4555
}
4656

4757
// will retry in intervals of 5, 10, 30

packages/replay/src/util/sendReplayRequest.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,17 @@ export async function sendReplayRequest({
115115

116116
try {
117117
response = await transport.send(envelope);
118-
} catch {
119-
throw new Error(UNABLE_TO_SEND_REPLAY);
118+
} catch (err) {
119+
const error = new Error(UNABLE_TO_SEND_REPLAY);
120+
121+
try {
122+
// In case browsers don't allow this property to be writable
123+
// @ts-ignore This needs lib es2022 and newer
124+
error.cause = err;
125+
} catch {
126+
// nothing to do
127+
}
128+
throw error;
120129
}
121130

122131
// TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore

packages/replay/test/integration/sendReplayEvent.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ describe('Integration | sendReplayEvent', () => {
391391
expect(spyHandleException).toHaveBeenCalledTimes(5);
392392
expect(spyHandleException).toHaveBeenLastCalledWith(new Error('Unable to send Replay - max retries exceeded'));
393393

394+
const spyHandleExceptionCall = spyHandleException.mock.calls;
395+
expect(spyHandleExceptionCall[spyHandleExceptionCall.length - 1][0].cause.message).toEqual(
396+
'Something bad happened',
397+
);
398+
394399
// No activity has occurred, session's last activity should remain the same
395400
expect(replay.session?.lastActivity).toBe(BASE_TIMESTAMP);
396401

0 commit comments

Comments
 (0)