RemoteException.handle() method
The live remote exception value, if available.
Signature
class RemoteException {
handle(): JSHandle | undefined;
}
Returns:
JSHandle | undefined
Remarks
Only available with CDP, when the browser reports a reference to the thrown value (objects and symbols) and Puppeteer can bind it to the worker, the main world of a frame or Puppeteer's own isolated world.
It is undefined:
-
for thrown primitives other than symbols;
-
for exceptions thrown in other isolated worlds, for example ones created by extensions or via CDP;
-
for exceptions thrown by code that Puppeteer injected, for example from a callback scheduled inside Page.evaluate(), because the browser does not report a remote exception object for them;
-
always with WebDriver BiDi.
The handle is disposed automatically right after the exception event has been dispatched so that the browser can release the exception. To keep using it beyond the synchronous part of your listener, call JSHandle.move synchronously inside the listener and dispose of the handle yourself once you are done with it:
page.on('exception', async exception => {
// Must be called before the first `await`.
const handle = exception.handle()?.move();
if (!handle) {
return;
}
try {
const code = await handle.evaluate(error => {
return (error as {code?: string}).code;
});
console.log(code);
} finally {
await handle.dispose();
}
});
Using the handle after an await without calling move() first will throw because the handle has already been disposed.
The handle is shared by all listeners of the event: calling move() returns the same JSHandle instance in every listener, and disposing it invalidates it for all of them. If more than one listener needs the handle after the event, have only one of them call move() and dispose of it.