feat(rpc): update Response.finished to return string instead of Error (#3071)

This commit is contained in:
Dmitry Gozman 2020-07-21 14:40:53 -07:00 committed by GitHub
parent 47e30f047b
commit 3dd61629e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 18 additions and 19 deletions

View File

@ -243,7 +243,7 @@ export class CRNetworkManager {
_handleRequestRedirect(request: InterceptableRequest, responsePayload: Protocol.Network.Response) {
const response = this._createResponse(request, responsePayload);
response._requestFinished(new Error('Response body is unavailable for redirect responses'));
response._requestFinished('Response body is unavailable for redirect responses');
this._requestIdToRequest.delete(request._requestId);
if (request._interceptionId)
this._attemptedAuthentications.delete(request._interceptionId);

View File

@ -90,7 +90,7 @@ export class FFNetworkManager {
// Keep redirected requests in the map for future reference as redirectedFrom.
const isRedirected = response.status() >= 300 && response.status() <= 399;
if (isRedirected) {
response._requestFinished(new Error('Response body is unavailable for redirect responses'));
response._requestFinished('Response body is unavailable for redirect responses');
} else {
this._requests.delete(request._id);
response._requestFinished();

View File

@ -234,8 +234,8 @@ type GetResponseBodyCallback = () => Promise<Buffer>;
export class Response {
private _request: Request;
private _contentPromise: Promise<Buffer> | null = null;
_finishedPromise: Promise<Error | null>;
private _finishedPromiseCallback: any;
_finishedPromise: Promise<{ error?: string }>;
private _finishedPromiseCallback: (arg: { error?: string }) => void = () => {};
private _status: number;
private _statusText: string;
private _url: string;
@ -255,8 +255,8 @@ export class Response {
this._request._setResponse(this);
}
_requestFinished(error?: Error) {
this._finishedPromiseCallback.call(null, error || null);
_requestFinished(error?: string) {
this._finishedPromiseCallback({ error });
}
url(): string {
@ -280,14 +280,14 @@ export class Response {
}
finished(): Promise<Error | null> {
return this._finishedPromise;
return this._finishedPromise.then(({ error }) => error ? new Error(error) : null);
}
body(): Promise<Buffer> {
if (!this._contentPromise) {
this._contentPromise = this._finishedPromise.then(async error => {
this._contentPromise = this._finishedPromise.then(async ({ error }) => {
if (error)
throw error;
throw new Error(error);
return this._getResponseBodyCallback();
});
}

View File

@ -1472,7 +1472,7 @@ export type ResponseBodyResult = {
};
export type ResponseFinishedParams = {};
export type ResponseFinishedResult = {
error?: SerializedError,
error?: string,
};
// ----------- ConsoleMessage -----------

View File

@ -19,7 +19,7 @@ import * as types from '../../types';
import { RequestChannel, ResponseChannel, RouteChannel, RequestInitializer, ResponseInitializer, RouteInitializer } from '../channels';
import { ChannelOwner } from './channelOwner';
import { Frame } from './frame';
import { normalizeFulfillParameters, headersArrayToObject, normalizeContinueOverrides, parseError } from '../serializers';
import { normalizeFulfillParameters, headersArrayToObject, normalizeContinueOverrides } from '../serializers';
export type NetworkCookie = {
name: string,
@ -206,7 +206,7 @@ export class Response extends ChannelOwner<ResponseChannel, ResponseInitializer>
async finished(): Promise<Error | null> {
const result = await this._channel.finished();
if (result.error)
return parseError(result.error);
return new Error(result.error);
return null;
}

View File

@ -1340,7 +1340,7 @@ interface Response
command finished
returns
error?: SerializedError
error?: string
interface ConsoleMessage
initializer

View File

@ -15,10 +15,10 @@
*/
import { Request, Response, Route } from '../../network';
import { RequestChannel, ResponseChannel, RouteChannel, ResponseInitializer, RequestInitializer, RouteInitializer, Binary, SerializedError } from '../channels';
import { RequestChannel, ResponseChannel, RouteChannel, ResponseInitializer, RequestInitializer, RouteInitializer, Binary } from '../channels';
import { Dispatcher, DispatcherScope, lookupNullableDispatcher, existingDispatcher } from './dispatcher';
import { FrameDispatcher } from './frameDispatcher';
import { headersObjectToArray, headersArrayToObject, serializeError } from '../serializers';
import { headersObjectToArray, headersArrayToObject } from '../serializers';
import * as types from '../../types';
export class RequestDispatcher extends Dispatcher<Request, RequestInitializer> implements RequestChannel {
@ -64,9 +64,8 @@ export class ResponseDispatcher extends Dispatcher<Response, ResponseInitializer
});
}
async finished(): Promise<{ error?: SerializedError }> {
const error = await this._object.finished();
return { error: error ? serializeError(error) : undefined };
async finished(): Promise<{ error?: string }> {
return await this._object._finishedPromise;
}
async body(): Promise<{ binary: Binary }> {

View File

@ -853,7 +853,7 @@ export class WKPage implements PageDelegate {
private _handleRequestRedirect(request: WKInterceptableRequest, responsePayload: Protocol.Network.Response) {
const response = request.createResponse(responsePayload);
response._requestFinished(new Error('Response body is unavailable for redirect responses'));
response._requestFinished('Response body is unavailable for redirect responses');
this._requestIdToRequest.delete(request._requestId);
this._page._frameManager.requestReceivedResponse(response);
this._page._frameManager.requestFinished(request.request);