feat(Request): allow aborting intercepted requests with custom reasons (#1080)

This patch adds optional parameter to the `request.abort()` method
that specifies abort reason.

References #1020.
This commit is contained in:
Andrey Lushnikov
2017-10-18 00:26:48 -07:00
committed by GitHub
parent 7fbd2cb661
commit 4f64dfd993
3 changed files with 52 additions and 4 deletions

View File

@@ -340,16 +340,21 @@ class Request {
});
}
async abort() {
/**
* @param {string=} errorCode
*/
async abort(errorCode = 'failed') {
// DataURL's are not interceptable. In this case, do nothing.
if (this.url.startsWith('data:'))
return;
const errorReason = errorReasons[errorCode];
console.assert(errorReason, 'Unknown error code: ' + errorCode);
console.assert(this._allowInterception, 'Request Interception is not enabled!');
console.assert(!this._interceptionHandled, 'Request is already handled!');
this._interceptionHandled = true;
await this._client.send('Network.continueInterceptedRequest', {
interceptionId: this._interceptionId,
errorReason: 'Failed'
errorReason
}).catch(error => {
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
@@ -357,6 +362,22 @@ class Request {
});
}
}
const errorReasons = {
'aborted': 'Aborted',
'accessdenied': 'AccessDenied',
'addressunreachable': 'AddressUnreachable',
'connectionaborted': 'ConnectionAborted',
'connectionclosed': 'ConnectionClosed',
'connectionfailed': 'ConnectionFailed',
'connectionrefused': 'ConnectionRefused',
'connectionreset': 'ConnectionReset',
'internetdisconnected': 'InternetDisconnected',
'namenotresolved': 'NameNotResolved',
'timedout': 'TimedOut',
'failed': 'Failed',
};
helper.tracePublicAPI(Request);
class Response {