chore(fetch): include response text into failOnStatusCode errors (#31978)
Fixes https://github.com/microsoft/playwright/issues/31834
This commit is contained in:
parent
5a83fe55bc
commit
db0980a850
|
|
@ -214,8 +214,16 @@ export abstract class APIRequestContext extends SdkObject {
|
||||||
});
|
});
|
||||||
const fetchUid = this._storeResponseBody(fetchResponse.body);
|
const fetchUid = this._storeResponseBody(fetchResponse.body);
|
||||||
this.fetchLog.set(fetchUid, controller.metadata.log);
|
this.fetchLog.set(fetchUid, controller.metadata.log);
|
||||||
if (params.failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400))
|
if (params.failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400)) {
|
||||||
throw new Error(`${fetchResponse.status} ${fetchResponse.statusText}`);
|
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 };
|
return { ...fetchResponse, fetchUid };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,8 @@ for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']
|
||||||
failOnStatusCode: true
|
failOnStatusCode: true
|
||||||
}).catch(e => e);
|
}).catch(e => e);
|
||||||
expect(error.message).toContain('404 Not Found');
|
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 }) => {
|
it(`${method}should support ignoreHTTPSErrors option`, async ({ context, httpsServer }) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue