Javascript 如何从连接到Puppeter页面的pageerror侦听器捕获错误?

Javascript 如何从连接到Puppeter页面的pageerror侦听器捕获错误?,javascript,async-await,event-handling,try-catch,puppeteer,Javascript,Async Await,Event Handling,Try Catch,Puppeteer,我正在用木偶演员写一个预渲染器 我添加了一个功能,允许用户切换正在呈现的页面中的错误是否会导致预呈现停止(在出现这种情况时通过拒绝承诺的方式) 我的问题是我不能抓住被拒绝的承诺 这是我的密码: async getMarkup(路由){ const s=this.useHttps==true?'s':''; constURL=`http${s}://localhost:${this.port}${route}`; const page=等待this.browser.newPage(); page.

我正在用木偶演员写一个预渲染器

我添加了一个功能,允许用户切换正在呈现的页面中的错误是否会导致预呈现停止(在出现这种情况时通过拒绝承诺的方式)

我的问题是我不能抓住被拒绝的承诺

这是我的密码:

async getMarkup(路由){
const s=this.useHttps==true?'s':'';
constURL=`http${s}://localhost:${this.port}${route}`;
const page=等待this.browser.newPage();
page.on('pageerror',函数(err){
控制台日志(err);
拒绝(错误);
});
试一试{
等待page.goto(url,{timeout:60000});
if(this.waitForElement!==null){
wait page.waitForSelector(this.waitForElement,{timeout:60000});
}
返回等待页面.content();
}捕捉(错误){
犯错误;
}
}
我通过在脚本中故意出现语法错误的路由上调用它来测试这一点

处理程序已附加,但我无法捕获被拒绝的承诺。这是(截断的)输出:


resolve
reject
应在具有功能的承诺中使用(例如
promise.resolve
)。另外,

newpromise((解析,拒绝)=>setTimeout(解析,1000))
//及
新承诺((x,y)=>setTimeout(x,1000))
都是相同的,因此您需要承诺
拒绝
才有意义

捕获后,您将再次抛出错误。除非您在
getMarkup
之外的其他地方捕获它,否则您将看到相同的警告

试试看{
等待page.goto(url,{timeout:60000});
if(this.waitForElement!==null)
wait page.waitForSelector(this.waitForElement,{timeout:60000});
返回等待页面.content();
}捕捉(错误){
//这里
控制台错误(错误消息);
}

最后,如果错误发生在网页的Javascript中,我建议在页面中使用
window.onerror
事件。您可以找到更多信息。

resolve
reject
应在具有功能的承诺中使用(例如
promise.resolve
)。另外,

newpromise((解析,拒绝)=>setTimeout(解析,1000))
//及
新承诺((x,y)=>setTimeout(x,1000))
都是相同的,因此您需要承诺
拒绝
才有意义

捕获后,您将再次抛出错误。除非您在
getMarkup
之外的其他地方捕获它,否则您将看到相同的警告

试试看{
等待page.goto(url,{timeout:60000});
if(this.waitForElement!==null)
wait page.waitForSelector(this.waitForElement,{timeout:60000});
返回等待页面.content();
}捕捉(错误){
//这里
控制台错误(错误消息);
}

最后,如果错误发生在网页的Javascript中,我建议在页面中使用
window.onerror
事件。你可以找到更多信息。

拒绝
是有意义的,当你有承诺时,我在这里没有看到任何承诺。如果你想捕获网页中的javascript错误,你不能像这样捕获它。感谢你的响应——我知道,任何对以
async
为前缀的方法的调用都会隐式地返回承诺。在这种情况下,虽然页面最初是同步分配的,并且事件处理程序是同步附加的,但是
unhandledPromisejectionWarning
发起的调用(
wait page.goto(
)是异步调用的,因此返回一个承诺。我也尝试过简单地重试错误,尽管这会产生类似的响应(请参见我最近添加的编辑#1)当你有承诺时,
reject
是有意义的,我在这里看不到任何承诺。如果你想捕获网页中的javascript错误,你不能像这样捕获它感谢你的响应-我理解任何对前缀为
async
的方法的调用都会隐式返回承诺。在这种情况下,虽然页面最初被分配,并且以同步方式连接的vent处理程序,
unhandledPromisejectionWarning
发起的调用(
wait page.goto(
)是异步调用的,因此会返回一个承诺。我也尝试过简单地重试错误,尽管这会产生类似的响应(请参阅我最近添加的edit#1)。
[Error: TypeError: (intermediate value).noSuchMethod is not a function
    at window.onload (https://localhost:3005/error_in_js.html:19:41)]
(node:61704) UnhandledPromiseRejectionWarning: ReferenceError: reject is not defined
    at Page.<anonymous> (/Users/andy/repos/ewh-open-source/spa-prerenderer/index.js:164:7)
    at Page.emit (events.js:321:20)
    at Page._handleException (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/puppeteer/lib/Page.js:526:10)
    at CDPSession.<anonymous> (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/puppeteer/lib/Page.js:123:60)
    at CDPSession.emit (events.js:321:20)
    at CDPSession._onMessage (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/puppeteer/lib/Connection.js:200:12)
    at Connection._onMessage (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/puppeteer/lib/Connection.js:112:17)
    at WebSocket.<anonymous> (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/puppeteer/lib/WebSocketTransport.js:44:24)
    at WebSocket.onMessage (/Users/andy/repos/ewh-open-source/spa-prerenderer/node_modules/ws/lib/event-target.js:120:16)
[Error: TypeError: (intermediate value).noSuchMethod is not a function
    at window.onload (https://localhost:3000/error_in_js.html:19:41)]
(node:16112) UnhandledPromiseRejectionWarning
(node:16112) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16112) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.