Chrome DevTools for coding agents
fix: make request and response handling more robust (#846)
Alex Rudenko695817f6d6da
diff --git a/src/formatters/NetworkFormatter.ts b/src/formatters/NetworkFormatter.tsindex 7302cd4e..313e091c 100644--- a/src/formatters/NetworkFormatter.ts+++ b/src/formatters/NetworkFormatter.ts@@ -50,45 +50,36 @@ export class NetworkFormatter {async #loadDetailedData(): Promise<void> {// Load Request Bodyif (this.#request.hasPostData()) {- const data = this.#request.postData();- if (data) {- if (this.#options.requestFilePath) {- if (!this.#options.saveFile) {- throw new Error('saveFile is not provided');- }+ let data;+ try {+ data =+ this.#request.postData() ?? (await this.#request.fetchPostData());+ } catch {+ // Ignore parsing errors+ }+ const requestBodyNotAvailableMessage =+ '<Request body not available anymore>';+ if (this.#options.requestFilePath) {+ if (!this.#options.saveFile) {+ throw new Error('saveFile is not provided');+ }+ if (data) {await this.#options.saveFile(Buffer.from(data),this.#options.requestFilePath,);this.#requestBodyFilePath = this.#options.requestFilePath;} else {+ this.#requestBody = requestBodyNotAvailableMessage;+ }+ } else {+ if (data) {this.#requestBody = getSizeLimitedString(data,BODY_CONTEXT_SIZE_LIMIT,);- }- } else {- try {- const fetchData = await this.#request.fetchPostData();- if (fetchData) {- if (this.#options.requestFilePath) {- if (!this.#options.saveFile) {- throw new Error('saveFile is not provided');- }- await this.#options.saveFile(- Buffer.from(fetchData),- this.#options.requestFilePath,- );- this.#requestBodyFilePath = this.#options.requestFilePath;- } else {- this.#requestBody = getSizeLimitedString(- fetchData,- BODY_CONTEXT_SIZE_LIMIT,- );- }- }- } catch {- this.#requestBody = '<not available anymore>';+ } else {+ this.#requestBody = requestBodyNotAvailableMessage;}}}@@ -96,11 +87,23 @@ export class NetworkFormatter {// Load Response Bodyconst response = this.#request.response();if (response) {+ const responseBodyNotAvailableMessage =+ '<Response body not available anymore>';if (this.#options.responseFilePath) {- this.#responseBodyFilePath = await this.#saveResponseBodyToFile(- response,- this.#options.responseFilePath,- );+ try {+ const buffer = await response.buffer();+ if (!this.#options.saveFile) {+ throw new Error('saveFile is not provided');+ }+ await this.#options.saveFile(buffer, this.#options.responseFilePath);+ this.#responseBodyFilePath = this.#options.responseFilePath;+ } catch {+ // Flatten error handling for buffer() failure and save failure+ }++ if (!this.#responseBodyFilePath) {+ this.#responseBody = responseBodyNotAvailableMessage;+ }} else {this.#responseBody = await this.#getFormattedResponseBody(response,@@ -262,22 +265,6 @@ export class NetworkFormatter {return '<not available anymore>';}}-- async #saveResponseBodyToFile(- httpResponse: HTTPResponse,- filePath: string,- ): Promise<string> {- try {- const responseBuffer = await httpResponse.buffer();- if (!this.#options.saveFile) {- throw new Error('saveFile is not provided');- }- await this.#options.saveFile(responseBuffer, filePath);- return filePath;- } catch {- return '<not available anymore>';- }- }}function getSizeLimitedString(text: string, sizeLimit: number) {diff --git a/tests/formatters/NetworkFormatter.test.ts b/tests/formatters/NetworkFormatter.test.tsindex 94fa79f0..a1f1fd3c 100644--- a/tests/formatters/NetworkFormatter.test.ts+++ b/tests/formatters/NetworkFormatter.test.ts@@ -328,6 +328,48 @@ describe('NetworkFormatter', () => {assert.ok(result.includes(`Saved to ${reqPath}.`));assert.ok(result.includes(`Saved to ${resPath}.`));});++ it('handles missing bodies with filepath', async () => {+ const request = {+ method: () => 'POST',+ url: () => 'http://example.com',+ headers: () => ({}),+ hasPostData: () => true, // Claim we have data+ postData: () => null, // But returns null+ response: () => ({+ status: () => 200,+ headers: () => ({}),+ buffer: async () => {+ throw new Error('Body not available');+ },+ }),+ failure: () => null,+ redirectChain: () => [],+ fetchPostData: async () => {+ throw new Error('Body not available');+ },+ } as unknown as HTTPRequest;++ const reqPath = join(tmpDir, 'req_missing.txt');+ const resPath = join(tmpDir, 'res_missing.txt');++ const formatter = await NetworkFormatter.from(request, {+ fetchData: true,+ requestFilePath: reqPath,+ responseFilePath: resPath,+ saveFile: async (data, filename) => {+ await writeFile(filename, data);+ return {filename};+ },+ });++ const result = formatter.toStringDetailed();+ assert.ok(+ result.includes(+ `### Response Body\n<Response body not available anymore>`,+ ),+ );+ });});describe('toJSON', () => {