Skip to content

Commit a7f3f1c

Browse files
committed
Store original encoded data along with decoding errors if body decoding fails
1 parent 117410e commit a7f3f1c

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

src/model/http/exchange.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ export class HttpBody implements MessageBody {
127127

128128
private _decoded: Buffer | undefined;
129129

130+
@observable
131+
decodingError: Error | undefined;
132+
130133
decodedPromise: ObservablePromise<Buffer | undefined> = lazyObservablePromise(async () => {
131134
// Exactly one of _encoded & _decoded is a buffer, never neither/both.
132135
if (this._decoded) return this._decoded;
@@ -140,8 +143,17 @@ export class HttpBody implements MessageBody {
140143
const { decoded, encoded } = await decodeBody(encodedBuffer, this._contentEncoding);
141144
this._encoded = encoded;
142145
return decoded;
143-
} catch (e) {
146+
} catch (e: any) {
144147
reportError(e);
148+
149+
// In most cases, we get the encoded data back regardless, so recapture it here:
150+
if (e.inputBuffer) {
151+
this._encoded = e.inputBuffer;
152+
}
153+
runInAction(() => {
154+
this.decodingError = e;
155+
});
156+
145157
return undefined;
146158
}
147159
});
@@ -160,6 +172,7 @@ export class HttpBody implements MessageBody {
160172
// Set to a valid state for an un-decoded but totally empty body.
161173
this._decoded = undefined;
162174
this._encoded = emptyBuffer;
175+
this.decodingError = undefined;
163176
this.decodedPromise = observablePromise(Promise.resolve(emptyBuffer));
164177
}
165178
}

src/services/ui-worker-api.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,25 @@ export async function decodeBody(encodedBuffer: Buffer, encodings: string[]) {
7878
return { encoded: encodedBuffer, decoded: encodedBuffer };
7979
}
8080

81-
const result = await callApi<DecodeRequest, DecodeResponse>({
82-
type: 'decode',
83-
buffer: encodedBuffer.buffer as ArrayBuffer,
84-
encodings
85-
}, [encodedBuffer.buffer]);
86-
87-
return {
88-
encoded: Buffer.from(result.inputBuffer),
89-
decoded: Buffer.from(result.decodedBuffer)
90-
};
81+
try {
82+
const result = await callApi<DecodeRequest, DecodeResponse>({
83+
type: 'decode',
84+
buffer: encodedBuffer.buffer as ArrayBuffer,
85+
encodings
86+
}, [encodedBuffer.buffer]);
87+
88+
return {
89+
encoded: Buffer.from(result.inputBuffer),
90+
decoded: Buffer.from(result.decodedBuffer)
91+
};
92+
} catch (e: any) {
93+
// In general, the worker should return the original encoded buffer to us, so we can
94+
// show it to the user to help them debug encoding issues:
95+
if (e.inputBuffer) {
96+
e.inputBuffer = Buffer.from(e.inputBuffer);
97+
}
98+
throw e;
99+
}
91100
}
92101

93102
export async function encodeBody(decodedBuffer: Buffer, encodings: string[]) {

src/services/ui-worker.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,22 @@ ctx.addEventListener('message', async (event: { data: BackgroundRequest }) => {
175175
try {
176176
switch (event.data.type) {
177177
case 'decode':
178-
const decodeResult = await decodeRequest(event.data);
179-
ctx.postMessage(decodeResult, [
180-
decodeResult.inputBuffer,
181-
decodeResult.decodedBuffer
182-
]);
178+
try {
179+
const decodeResult = await decodeRequest(event.data);
180+
ctx.postMessage(decodeResult, [
181+
decodeResult.inputBuffer,
182+
decodeResult.decodedBuffer
183+
]);
184+
} catch (e: any) {
185+
// Special case for decoding errors: we send the encoded data back with the error, so the user can debug it:
186+
ctx.postMessage({
187+
id: event.data.id,
188+
error: Object.assign(serializeError(e), {
189+
inputBuffer: event.data.buffer
190+
})
191+
}, [event.data.buffer]);
192+
return;
193+
}
183194
break;
184195

185196
case 'encode':

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export type MessageBody = {
129129
encoded: { byteLength: number } | Buffer,
130130
decoded: Buffer | undefined,
131131
decodedPromise: ObservablePromise<Buffer | undefined>,
132+
decodingError: Error | undefined,
132133
cleanup(): void
133134
};
134135

0 commit comments

Comments
 (0)