feat(Request): introduce Request.failure() method (#1065)

Similarly to the `request.response()` method, this patch adds
`request.failure()` method that returns error details for the failed
requests.

Fixes #901.
This commit is contained in:
Andrey Lushnikov 2017-10-17 17:48:04 -07:00 committed by GitHub
parent 8717203fb2
commit 273c733237
3 changed files with 30 additions and 0 deletions

View File

@ -159,6 +159,7 @@
- [class: Request](#class-request) - [class: Request](#class-request)
* [request.abort()](#requestabort) * [request.abort()](#requestabort)
* [request.continue([overrides])](#requestcontinueoverrides) * [request.continue([overrides])](#requestcontinueoverrides)
* [request.failure()](#requestfailure)
* [request.headers](#requestheaders) * [request.headers](#requestheaders)
* [request.method](#requestmethod) * [request.method](#requestmethod)
* [request.postData](#requestpostdata) * [request.postData](#requestpostdata)
@ -1817,6 +1818,21 @@ Exception is immediately thrown if the request interception is not enabled.
Continues request with optional request overrides. To use this, request interception should be enabled with `page.setRequestInterceptionEnabled`. Continues request with optional request overrides. To use this, request interception should be enabled with `page.setRequestInterceptionEnabled`.
Exception is immediately thrown if the request interception is not enabled. Exception is immediately thrown if the request interception is not enabled.
#### request.failure()
- returns: <[Object]> Object describing request failure, if any
- `errorText` <[string]> Human-readable error message, e.g. `'net::ERR_FAILED'`.
The method returns null unless this request was failed, as reported by
`requestfailed` event.
Example of logging all failed requests:
```js
page.on('requestfailed', request => {
console.log(request.url + ' ' + request.failure().errorText);
});
```
#### request.headers #### request.headers
- <[Object]> An object with HTTP headers associated with the request. All header names are lower-case. - <[Object]> An object with HTTP headers associated with the request. All header names are lower-case.

View File

@ -259,6 +259,7 @@ class NetworkManager extends EventEmitter {
// @see https://crbug.com/750469 // @see https://crbug.com/750469
if (!request) if (!request)
return; return;
request._failureText = event.errorText;
request._completePromiseFulfill.call(null); request._completePromiseFulfill.call(null);
this._requestIdToRequest.delete(request._requestId); this._requestIdToRequest.delete(request._requestId);
this._interceptionIdToRequest.delete(request._interceptionId); this._interceptionIdToRequest.delete(request._interceptionId);
@ -284,6 +285,7 @@ class Request {
this._allowInterception = allowInterception; this._allowInterception = allowInterception;
this._interceptionHandled = false; this._interceptionHandled = false;
this._response = null; this._response = null;
this._failureText = null;
this._completePromise = new Promise(fulfill => { this._completePromise = new Promise(fulfill => {
this._completePromiseFulfill = fulfill; this._completePromiseFulfill = fulfill;
}); });
@ -304,6 +306,17 @@ class Request {
return this._response; return this._response;
} }
/**
* @return {?{errorText: string}}
*/
failure() {
if (!this._failureText)
return null;
return {
errorText: this._failureText
};
}
/** /**
* @param {!Object=} overrides * @param {!Object=} overrides
*/ */

View File

@ -2125,6 +2125,7 @@ describe('Page', function() {
expect(failedRequests[0].url).toContain('one-style.css'); expect(failedRequests[0].url).toContain('one-style.css');
expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].response()).toBe(null);
expect(failedRequests[0].resourceType).toBe('stylesheet'); expect(failedRequests[0].resourceType).toBe('stylesheet');
expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED');
})); }));
it('Page.Events.RequestFinished', SX(async function() { it('Page.Events.RequestFinished', SX(async function() {
const requests = []; const requests = [];