对 https 使用 try-catch,但未捕获任何错误



我使用HTTPS从网站获取数据,我想做的是捕捉任何可能发生的错误,但是它什么也没捕获所以这是我的主代码

test = async() => {
console.log("Hellow")
now = new Date();
const https = require("https");
https.get("website",{ agent: proxyy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
});
res.on("end", function () {
var resp = JSON.parse(body);
data_wanted = resp.data
const desiredItem = data_wanted.find((item) =>
item.name.includes("data")
);
console.log(desiredItem)
});
}
);
};

我尝试了多种方法来捕获错误,像这样

async function run() {
try {
await test();
} catch (error) {
console.log(error)
}

async function f() {
try{
run = await test()
}catch(e){
console.log("Hello world")
}

}

它尝试在函数内使用try-catch,但也没有工作,我最好的猜测是在函数完成抓取

之前执行try-catch。编辑1:所以我真正的意图是做一个while循环,不断尝试,直到没有错误

const https = require('https');
https.get('https://encrypted.google.com/', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error(e);
});

https://nodejs.org/api/https.html https_https_get_options_callback

相关内容

最新更新