[英]using try-catch for https but don't catch any error
我正在使用 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")
}
}
it tried using the try-catch inside the function but also didn't works, my best guess that the try-catch is being executed before the function finish fetching
编辑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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.