Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap event pre-processing in try/catch loop to catch potential errors #102

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions packages/node/src/retry/defaultRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ export class RetryHandler extends BaseRetryHandler {
*/
public async sendEventsWithRetry(events: readonly Event[]): Promise<Response> {
let response: Response = { status: Status.Unknown, statusCode: 0 };
const eventsToSend = this._pruneEvents(events);
let eventsToSend: Event[] = [];
try {
eventsToSend = this._pruneEvents(events);
response = await this._transport.sendPayload(this._getPayload(eventsToSend));
if (response.status !== Status.Success) {
throw new Error(response.status);
}
} catch {
if (this._shouldRetryEvents()) {
this._onEventsError(events, response);
this._onEventsError(eventsToSend, response);
}
}

Expand All @@ -71,19 +72,21 @@ export class RetryHandler extends BaseRetryHandler {
// to the retry buffer they should be in
private _pruneEvents(events: readonly Event[]): Event[] {
const prunedEvents: Event[] = [];
events.forEach(event => {
const { user_id: userId = '', device_id: deviceId = '' } = event;
// We can ignore events with neither. They would fail anyways when sent as event.
if (userId.length > 0 || deviceId.length > 0) {
const retryBuffer = this._getRetryBuffer(userId, deviceId);
if (retryBuffer !== null) {
retryBuffer.push(event);
this._eventsInRetry++;
} else {
prunedEvents.push(event);
if (Array.isArray(events)) {
for (const event of events) {
const { user_id: userId = '', device_id: deviceId = '' } = event;
// We can ignore events with neither. They would fail anyways when sent as event.
if (userId.length > 0 || deviceId.length > 0) {
const retryBuffer = this._getRetryBuffer(userId, deviceId);
if (retryBuffer !== null) {
retryBuffer.push(event);
this._eventsInRetry++;
} else {
prunedEvents.push(event);
}
}
}
});
}

return prunedEvents;
}
Expand Down