chore(fetch): include response text into failOnStatusCode errors (#31978)

Fixes https://github.com/microsoft/playwright/issues/31834
This commit is contained in:
Yury Semikhatsky 2024-08-01 17:53:43 -07:00 committed by GitHub
parent 5a83fe55bc
commit db0980a850
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View File

@ -214,8 +214,16 @@ export abstract class APIRequestContext extends SdkObject {
});
const fetchUid = this._storeResponseBody(fetchResponse.body);
this.fetchLog.set(fetchUid, controller.metadata.log);
if (params.failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400))
throw new Error(`${fetchResponse.status} ${fetchResponse.statusText}`);
if (params.failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400)) {
let responseText = '';
if (fetchResponse.body.byteLength) {
let text = fetchResponse.body.toString('utf8');
if (text.length > 1000)
text = text.substring(0, 997) + '...';
responseText = `\nResponse text:\n${text}`;
}
throw new Error(`${fetchResponse.status} ${fetchResponse.statusText}${responseText}`);
}
return { ...fetchResponse, fetchUid };
}

View File

@ -145,6 +145,8 @@ for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']
failOnStatusCode: true
}).catch(e => e);
expect(error.message).toContain('404 Not Found');
if (method !== 'head')
expect(error.message).toContain('Response text:\nFile not found:');
});
it(`${method}should support ignoreHTTPSErrors option`, async ({ context, httpsServer }) => {