[weather] add error handling to weather fetch functions, including cors (#3791)

Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
fixes #3687
This commit is contained in:
sam detweiler
2025-11-08 07:21:31 -06:00
committed by GitHub
parent 3b79791a6b
commit c1aaea5913
5 changed files with 71 additions and 56 deletions

View File

@@ -17,19 +17,29 @@ async function performWebRequest (url, type = "json", useCorsProxy = false, requ
requestUrl = url;
request.headers = getHeadersToSend(requestHeaders);
}
const response = await fetch(requestUrl, request);
const data = await response.text();
if (type === "xml") {
return new DOMParser().parseFromString(data, "text/html");
} else {
if (!data || !data.length > 0) return undefined;
try {
const response = await fetch(requestUrl, request);
if (response.ok) {
const data = await response.text();
const dataResponse = JSON.parse(data);
if (!dataResponse.headers) {
dataResponse.headers = getHeadersFromResponse(expectedResponseHeaders, response);
if (type === "xml") {
return new DOMParser().parseFromString(data, "text/html");
} else {
if (!data || !data.length > 0) return undefined;
const dataResponse = JSON.parse(data);
if (!dataResponse.headers) {
dataResponse.headers = getHeadersFromResponse(expectedResponseHeaders, response);
}
return dataResponse;
}
} else {
throw new Error(`Response status: ${response.status}`);
}
return dataResponse;
} catch (error) {
Log.error(`Error fetching data from ${url}: ${error}`);
return undefined;
}
}