improve error handling

Make sure all errors are able to return their traces to be displayed, and that only the code appears in the pop-up, not a mix with the actual content.
This commit is contained in:
buzzcode2007 2024-04-02 22:35:48 +08:00
parent 76bf6cefaa
commit e152034eeb
3 changed files with 81 additions and 79 deletions

View file

@ -1,5 +1,5 @@
/* net.js
This script provides network utilities.
This script provides network utilities.
*/
/*
@ -11,31 +11,35 @@ Download a file from the network or locally.
@returns {Promise} the downloaded file
*/
export async function download(URL, type, verify_only = false) {
const alert = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))
.default;
const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`)))
.default;
const alert = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`))
.default;
const texts = (await import(chrome.runtime.getURL(`gui/scripts/read.js`)))
.default;
let connect = await fetch(URL),
data;
let connect = await fetch(URL),
data;
if (connect.ok && !verify_only) {
data = await connect.text();
if (connect.ok && !verify_only) {
try {
data = await connect.text();
if (
type
? type.toLowerCase().includes(`json`) || type.toLowerCase().includes(`dictionary`)
: false
) {
try {
data = JSON.parse(data);
// When not in JSON, run this.
} catch(err) {
throw new TypeError(texts.localized(`error_msg_notJSON`, false));
};
};
} catch(err) {
alert.error(err.name, err.message, err.stack);
}
}
try {
} catch (err) {
if (
type
? type.toLowerCase().includes(`json`) ||
type.toLowerCase().includes(`dictionary`)
: false
) {
data = JSON.parse(data);
}
alert.error(texts.localized(`error_msg_notJSON`, false));
}
}
// Return the filter.
return verify_only ? connect.ok : data;
// Return the filter.
return verify_only ? connect.ok : data;
}