mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user