@@ -50,57 +50,60 @@ export class NetworkFormatter {
5050 async #loadDetailedData( ) : Promise < void > {
5151 // Load Request Body
5252 if ( this . #request. hasPostData ( ) ) {
53- const data = this . #request. postData ( ) ;
54- if ( data ) {
55- if ( this . #options. requestFilePath ) {
56- if ( ! this . #options. saveFile ) {
57- throw new Error ( 'saveFile is not provided' ) ;
58- }
53+ let data ;
54+ try {
55+ data =
56+ this . #request. postData ( ) ?? ( await this . #request. fetchPostData ( ) ) ;
57+ } catch {
58+ // Ignore parsing errors
59+ }
60+ const requestBodyNotAvailableMessage =
61+ '<Request body not available anymore>' ;
62+ if ( this . #options. requestFilePath ) {
63+ if ( ! this . #options. saveFile ) {
64+ throw new Error ( 'saveFile is not provided' ) ;
65+ }
66+ if ( data ) {
5967 await this . #options. saveFile (
6068 Buffer . from ( data ) ,
6169 this . #options. requestFilePath ,
6270 ) ;
6371 this . #requestBodyFilePath = this . #options. requestFilePath ;
6472 } else {
73+ this . #requestBody = requestBodyNotAvailableMessage ;
74+ }
75+ } else {
76+ if ( data ) {
6577 this . #requestBody = getSizeLimitedString (
6678 data ,
6779 BODY_CONTEXT_SIZE_LIMIT ,
6880 ) ;
69- }
70- } else {
71- try {
72- const fetchData = await this . #request. fetchPostData ( ) ;
73- if ( fetchData ) {
74- if ( this . #options. requestFilePath ) {
75- if ( ! this . #options. saveFile ) {
76- throw new Error ( 'saveFile is not provided' ) ;
77- }
78- await this . #options. saveFile (
79- Buffer . from ( fetchData ) ,
80- this . #options. requestFilePath ,
81- ) ;
82- this . #requestBodyFilePath = this . #options. requestFilePath ;
83- } else {
84- this . #requestBody = getSizeLimitedString (
85- fetchData ,
86- BODY_CONTEXT_SIZE_LIMIT ,
87- ) ;
88- }
89- }
90- } catch {
91- this . #requestBody = '<not available anymore>' ;
81+ } else {
82+ this . #requestBody = requestBodyNotAvailableMessage ;
9283 }
9384 }
9485 }
9586
9687 // Load Response Body
9788 const response = this . #request. response ( ) ;
9889 if ( response ) {
90+ const responseBodyNotAvailableMessage =
91+ '<Response body not available anymore>' ;
9992 if ( this . #options. responseFilePath ) {
100- this . #responseBodyFilePath = await this . #saveResponseBodyToFile(
101- response ,
102- this . #options. responseFilePath ,
103- ) ;
93+ try {
94+ const buffer = await response . buffer ( ) ;
95+ if ( ! this . #options. saveFile ) {
96+ throw new Error ( 'saveFile is not provided' ) ;
97+ }
98+ await this . #options. saveFile ( buffer , this . #options. responseFilePath ) ;
99+ this . #responseBodyFilePath = this . #options. responseFilePath ;
100+ } catch {
101+ // Flatten error handling for buffer() failure and save failure
102+ }
103+
104+ if ( ! this . #responseBodyFilePath) {
105+ this . #responseBody = responseBodyNotAvailableMessage ;
106+ }
104107 } else {
105108 this . #responseBody = await this . #getFormattedResponseBody(
106109 response ,
@@ -262,22 +265,6 @@ export class NetworkFormatter {
262265 return '<not available anymore>' ;
263266 }
264267 }
265-
266- async #saveResponseBodyToFile(
267- httpResponse : HTTPResponse ,
268- filePath : string ,
269- ) : Promise < string > {
270- try {
271- const responseBuffer = await httpResponse . buffer ( ) ;
272- if ( ! this . #options. saveFile ) {
273- throw new Error ( 'saveFile is not provided' ) ;
274- }
275- await this . #options. saveFile ( responseBuffer , filePath ) ;
276- return filePath ;
277- } catch {
278- return '<not available anymore>' ;
279- }
280- }
281268}
282269
283270function getSizeLimitedString ( text : string , sizeLimit : number ) {
0 commit comments