Skip to content

Commit

Permalink
Fix timestamps for Send errors
Browse files Browse the repository at this point in the history
Previously, failed requests lost their start timing data (which wasn't
preserved from partial results when errors were thrown) and ignored the
timestamp for the failure itself entirely. Additionally, start times
could be entirely unset in some plausible cases if an error were thrown
before request-start fired. This is now handled more carefully, to
ensure that error durations are tracked correctly.
  • Loading branch information
pimterry committed Mar 11, 2024
1 parent a018fee commit fe91a32
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/model/send/send-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,21 @@ export class SendStore {
headers: rawHeadersToHeaders(requestInput.headers),
rawHeaders: requestInput.headers,
body: { buffer: encodedBody },
timingEvents: {} as TimingEvents,
timingEvents: {
startTime: Date.now()
} as TimingEvents,
tags: ['httptoolkit:manually-sent-request']
});

// Keep the exchange up to date as response data arrives:
trackResponseEvents(responseStream, exchange)
.catch(action((error: ErrorLike) => {
.catch(action((error: ErrorLike & { timingEvents?: TimingEvents }) => {
exchange.markAborted({
id: exchange.id,
error: error,
timingEvents: {
...exchange.timingEvents as TimingEvents,
abortedTimestamp: performance.now()
...error.timingEvents
},
tags: error.code ? [`passthrough-error:${error.code}`] : []
});
Expand Down Expand Up @@ -152,7 +154,6 @@ const trackResponseEvents = flow(function * (
const messageType = value.type;
switch (messageType) {
case 'request-start':
timingEvents.startTime = Date.now();
timingEvents.startTimestamp = value.timestamp;
timingEvents.bodyReceivedTimestamp = value.timestamp;
break;
Expand Down Expand Up @@ -182,11 +183,15 @@ const trackResponseEvents = flow(function * (
break;
case 'error':
if (value.error.message) {
timingEvents.startTimestamp ??= value.timestamp; // If request not yet started
timingEvents.abortedTimestamp = value.timestamp;

throw Object.assign(
new Error(value.error.message + (
value.error.code ? ` (${value.error.code})` : ''
)), {
code: value.error.code
code: value.error.code,
timingEvents
}
);
} else {
Expand Down

0 comments on commit fe91a32

Please sign in to comment.