【发布时间】:2018-07-17 14:27:30
【问题描述】:
有没有人尝试过使用 Puppeteer 处理网络连接错误?我尝试启动一个随机页面并检查在它工作之前是否没有收到任何错误(此尝试在 for 循环中):
try{
responseAwait = await page1.goto('https://www.residentadvisor.net/dj.aspx?country=01')
} catch (err) {
console.log('Page load fail : '+ err)
if (err == 'Error: net::ERR_INTERNET_DISCONNECTED' || err == 'Error: net::ERR_NETWORK_CHANGED' || err == 'Error: net::ERR_NAME_NOT_RESOLVED'){
let refreshIntervalId = setInterval(() =>{
handleConnexionError(refreshIntervalId,page1)
}, 5000)
}
}
这是我用来检查网络是否恢复的功能:
async function handleConnexionError(refreshIntervalId,page1){
console.log('Retrying to connect')
let errorHandle = true
await page1.goto('https://www.residentadvisor.net/dj.aspx?country=01').catch(() => {
errorHandle = false
})
if (errorHandle) {
console.log('Succesfully Reconnected')
clearInterval(refreshIntervalId)
return true
}
else {
console.log('Connection Fail Retrying in 10 sec ...')
}
}
但它无法正常工作,因为脚本继续运行并在整个 for 循环中循环,即使在 await 中发生错误...
【问题讨论】:
标签: javascript asynchronous async-await puppeteer