Skip to content

Commit

Permalink
chore: support raw error mode (#13339)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Nov 28, 2024
1 parent b0f082d commit ed67bd7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
14 changes: 14 additions & 0 deletions docs/api/puppeteer.connection._constructor_.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Connection {
transport: ConnectionTransport,
delay?: number,
timeout?: number,
rawErrors?: boolean,
);
}
```
Expand Down Expand Up @@ -81,5 +82,18 @@ number

_(Optional)_

</td></tr>
<tr><td>

rawErrors

</td><td>

boolean

</td><td>

_(Optional)_

</td></tr>
</tbody></table>
2 changes: 1 addition & 1 deletion docs/api/puppeteer.connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Description
</th></tr></thead>
<tbody><tr><td>
<span id="_constructor_">[(constructor)(url, transport, delay, timeout)](./puppeteer.connection._constructor_.md)</span>
<span id="_constructor_">[(constructor)(url, transport, delay, timeout, rawErrors)](./puppeteer.connection._constructor_.md)</span>
</td><td>
Expand Down
1 change: 1 addition & 0 deletions packages/puppeteer-core/src/bidi/BrowserConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async function getBiDiConnection(
connectionTransport,
slowMo,
protocolTimeout,
/* rawErrors= */ true,
);

const version = await cdpConnection.send('Browser.getVersion');
Expand Down
17 changes: 12 additions & 5 deletions packages/puppeteer-core/src/cdp/CDPSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class CdpCDPSession extends CDPSession {
#connection?: Connection;
#parentSessionId?: string;
#target?: CdpTarget;
#rawErrors = false;

/**
* @internal
Expand All @@ -40,12 +41,14 @@ export class CdpCDPSession extends CDPSession {
targetType: string,
sessionId: string,
parentSessionId: string | undefined,
rawErrors: boolean,
) {
super();
this.#connection = connection;
this.#targetType = targetType;
this.#sessionId = sessionId;
this.#parentSessionId = parentSessionId;
this.#rawErrors = rawErrors;
}

/**
Expand Down Expand Up @@ -113,11 +116,15 @@ export class CdpCDPSession extends CDPSession {
}): void {
if (object.id) {
if (object.error) {
this.#callbacks.reject(
object.id,
createProtocolErrorMessage(object),
object.error.message,
);
if (this.#rawErrors) {
this.#callbacks.rejectRaw(object.id, object.error);
} else {
this.#callbacks.reject(
object.id,
createProtocolErrorMessage(object),
object.error.message,
);
}
} else {
this.#callbacks.resolve(object.id, object.result);
}
Expand Down
21 changes: 15 additions & 6 deletions packages/puppeteer-core/src/cdp/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ export class Connection extends EventEmitter<CDPSessionEvents> {
#sessions = new Map<string, CdpCDPSession>();
#closed = false;
#manuallyAttached = new Set<string>();
#callbacks = new CallbackRegistry();
#callbacks: CallbackRegistry;
#rawErrors = false;

constructor(
url: string,
transport: ConnectionTransport,
delay = 0,
timeout?: number,
rawErrors = false,
) {
super();
this.#rawErrors = rawErrors;
this.#callbacks = new CallbackRegistry();
this.#url = url;
this.#delay = delay;
this.#timeout = timeout ?? 180_000;
Expand Down Expand Up @@ -159,6 +163,7 @@ export class Connection extends EventEmitter<CDPSessionEvents> {
object.params.targetInfo.type,
sessionId,
object.sessionId,
this.#rawErrors,
);
this.#sessions.set(sessionId, session);
this.emit(CDPSessionEvent.SessionAttached, session);
Expand All @@ -185,11 +190,15 @@ export class Connection extends EventEmitter<CDPSessionEvents> {
}
} else if (object.id) {
if (object.error) {
this.#callbacks.reject(
object.id,
createProtocolErrorMessage(object),
object.error.message,
);
if (this.#rawErrors) {
this.#callbacks.rejectRaw(object.id, object.error);
} else {
this.#callbacks.reject(
object.id,
createProtocolErrorMessage(object),
object.error.message,
);
}
} else {
this.#callbacks.resolve(object.id, object.result);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/puppeteer-core/src/common/CallbackRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export class CallbackRegistry {
this._reject(callback, message, originalMessage);
}

rejectRaw(id: number, error: object): void {
const callback = this.#callbacks.get(id);
if (!callback) {
return;
}
callback.reject(error as any);
}

_reject(
callback: Callback,
errorMessage: string | ProtocolError,
Expand Down

0 comments on commit ed67bd7

Please sign in to comment.