[英]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.