Skip to content

Chrome DevTools for coding agents

fix: propagate failed responseFilePath write error instead of reporting body unavailable (#2957) (#3119)

Yuri (IURII) Trukhin3cee3bd866c0
diff --git a/src/formatters/NetworkFormatter.ts b/src/formatters/NetworkFormatter.ts
index 71a86c64..bd488340 100644
--- a/src/formatters/NetworkFormatter.ts
+++ b/src/formatters/NetworkFormatter.ts
@@ -114,8 +114,14 @@ export class NetworkFormatter {
const responseBodyNotAvailableMessage =
'<Response body not available anymore>';
if (this.#options.responseFilePath) {
+ let buffer: Uint8Array | undefined;
try {
- const buffer = await response.buffer();
+ buffer = await response.buffer();
+ } catch {
+ // Response body is not available anymore
+ }
+
+ if (buffer) {
if (!this.#options.saveFile) {
throw new Error(
'Unable to save the response body to a file: no saveFile callback was configured.',
@@ -127,11 +133,7 @@ export class NetworkFormatter {
'.network-response',
);
this.#responseBodyFilePath = result.filename;
- } catch {
- // Flatten error handling for buffer() failure and save failure
- }
-
- if (!this.#responseBodyFilePath) {
+ } else {
this.#responseBody = responseBodyNotAvailableMessage;
}
} else {
diff --git a/tests/formatters/NetworkFormatter.test.ts b/tests/formatters/NetworkFormatter.test.ts
index 5bb82c7d..d52ee5a4 100644
--- a/tests/formatters/NetworkFormatter.test.ts
+++ b/tests/formatters/NetworkFormatter.test.ts
@@ -455,6 +455,37 @@ describe('NetworkFormatter', () => {
);
});
+ it('throws error when responseFilePath saveFile fails rather than reporting body unavailable', async () => {
+ const request = {
+ method: () => 'GET',
+ url: () => 'http://example.com/data',
+ headers: () => ({}),
+ hasPostData: () => false,
+ postData: () => undefined,
+ response: () => ({
+ status: () => 200,
+ headers: () => ({}),
+ buffer: async () => Buffer.from('response body data'),
+ }),
+ failure: () => null,
+ redirectChain: () => [],
+ fetchPostData: async () => undefined,
+ } as unknown as HTTPRequest;
+
+ await assert.rejects(async () => {
+ await NetworkFormatter.from(request, {
+ fetchData: true,
+ responseFilePath: '/unwritable/dir/blocked',
+ saveFile: async () => {
+ throw new Error(
+ 'EACCES: permission denied, open /unwritable/dir/blocked.network-response',
+ );
+ },
+ redactNetworkHeaders: false,
+ });
+ }, /EACCES: permission denied/);
+ });
+
it('handles missing response body when inline', async () => {
const response = getMockResponse();
response.buffer = () => Promise.reject(new Error('Evicted'));