-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsendReplay.ts
72 lines (61 loc) · 1.96 KB
/
sendReplay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { setTimeout } from '@sentry-internal/browser-utils';
import { setContext } from '@sentry/core';
import { RETRY_BASE_INTERVAL, RETRY_MAX_COUNT, UNABLE_TO_SEND_REPLAY } from '../constants';
import type { SendReplayData } from '../types';
import { RateLimitError, TransportStatusCodeError, sendReplayRequest } from './sendReplayRequest';
/**
* Finalize and send the current replay event to Sentry
*/
export async function sendReplay(
replayData: SendReplayData,
retryConfig = {
count: 0,
interval: RETRY_BASE_INTERVAL,
},
): Promise<unknown> {
const { recordingData, onError } = replayData;
// short circuit if there's no events to upload (this shouldn't happen as _runFlush makes this check)
if (!recordingData.length) {
return;
}
try {
await sendReplayRequest(replayData);
return true;
} catch (err) {
if (err instanceof TransportStatusCodeError || err instanceof RateLimitError) {
throw err;
}
// Capture error for every failed replay
setContext('Replays', {
_retryCount: retryConfig.count,
});
if (onError) {
onError(err);
}
// If an error happened here, it's likely that uploading the attachment
// failed, we'll can retry with the same events payload
if (retryConfig.count >= RETRY_MAX_COUNT) {
const error = new Error(`${UNABLE_TO_SEND_REPLAY} - max retries exceeded`);
try {
// In case browsers don't allow this property to be writable
// @ts-expect-error This needs lib es2022 and newer
error.cause = err;
} catch {
// nothing to do
}
throw error;
}
// will retry in intervals of 5, 10, 30
retryConfig.interval *= ++retryConfig.count;
return new Promise((resolve, reject) => {
setTimeout(async () => {
try {
await sendReplay(replayData, retryConfig);
resolve(true);
} catch (err) {
reject(err);
}
}, retryConfig.interval);
});
}
}