fix(js): handle errors gracefully

This commit is contained in:
Guanran Wang 2024-10-26 00:44:56 +08:00
parent f12b056107
commit aaaddb46ca
Signed by: nyancat
GPG key ID: 91F97D9ED12639CF
2 changed files with 50 additions and 30 deletions

View file

@ -37,7 +37,7 @@
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td class="table-cell">local</td> <td class="table-cell">ip.ny4.dev</td>
<td class="table-cell" id="check-ny4-ip">Loading...</td> <td class="table-cell" id="check-ny4-ip">Loading...</td>
<td class="table-cell" id="check-ny4-location">Loading...</td> <td class="table-cell" id="check-ny4-location">Loading...</td>
</tr> </tr>

View file

@ -1,45 +1,65 @@
async function main() { async function main() {
try { const [ny4, ipsb, speedtestcn, ipapiis] = await Promise.allSettled([
const [ny4, ipsb, speedtestcn, ipapiis] = await Promise.all([
fetchAsync("https://ip.ny4.dev/api/v1/ip"), fetchAsync("https://ip.ny4.dev/api/v1/ip"),
fetchAsync("https://api.ip.sb/geoip"), fetchAsync("https://api.ip.sb/geoip"),
fetchAsync("https://api-v3.speedtest.cn/ip"), fetchAsync("https://api-v3.speedtest.cn/ip"),
fetchAsync("https://api.ipapi.is/"), fetchAsync("https://api.ipapi.is/"),
]); ]);
updateDom("check-ny4-ip", ny4.ip); if (ny4.status === "fulfilled") {
updateDom("check-ny4-ip", ny4.value.ip);
updateDom("check-ny4-location", [ updateDom("check-ny4-location", [
ny4.city, ny4.value.city,
ny4.region, ny4.value.region,
ny4.country, ny4.value.country,
ny4.organization, ny4.value.organization,
]); ]);
} else {
updateDom("check-ny4-ip", "Fetch Failed");
updateDom("check-ny4-location", "Fetch Failed");
console.log("Fetch failed:", ny4.reason);
}
updateDom("check-ipsb-ip", ipsb.ip); if (ipsb.status === "fulfilled") {
updateDom("check-ipsb-ip", ipsb.value.ip);
updateDom("check-ipsb-location", [ updateDom("check-ipsb-location", [
ipsb.city, ipsb.value.city,
ipsb.region, ipsb.value.region,
ipsb.country, ipsb.value.country,
ipsb.isp, ipsb.value.isp,
]); ]);
} else {
updateDom("check-ipsb-ip", "Fetch Failed");
updateDom("check-ipsb-location", "Fetch Failed");
console.log("Fetch failed:", ipsb.reason);
}
updateDom("check-speedtestcn-ip", speedtestcn.data.ip); if (speedtestcn.status === "fulfilled") {
updateDom("check-speedtestcn-ip", speedtestcn.value.data.ip);
updateDom("check-speedtestcn-location", [ updateDom("check-speedtestcn-location", [
speedtestcn.data.city, speedtestcn.value.data.city,
speedtestcn.data.province, speedtestcn.value.data.province,
speedtestcn.data.country, speedtestcn.value.data.country,
speedtestcn.data.isp, speedtestcn.value.data.isp,
]); ]);
} else {
updateDom("check-speedtestcn-ip", "Fetch Failed");
updateDom("check-speedtestcn-location", "Fetch Failed");
console.log("Fetch failed:", speedtestcn.reason);
}
updateDom("check-ipapiis-ip", ipapiis.ip); if (ipapiis.status === "fulfilled") {
updateDom("check-ipapiis-ip", ipapiis.value.ip);
updateDom("check-ipapiis-location", [ updateDom("check-ipapiis-location", [
ipapiis.location.city, ipapiis.value.location.city,
ipapiis.location.state, ipapiis.value.location.state,
ipapiis.location.country, ipapiis.value.location.country,
ipapiis.company.name, ipapiis.value.company.name,
]); ]);
} catch (error) { } else {
console.log("Error fetching data:", error); updateDom("check-ipapiis-ip", "Fetch Failed");
updateDom("check-ipapiis-location", "Fetch Failed");
console.log("Fetch failed:", ipapiis.reason);
} }
} }