[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;
}
}

View File

@@ -22,38 +22,36 @@ WeatherProvider.register("pirateweather", {
lon: 0
},
fetchCurrentWeather () {
this.fetchData(this.getUrl())
.then((data) => {
if (!data || !data.currently || typeof data.currently.temperature === "undefined") {
// No usable data?
return;
}
async fetchCurrentWeather () {
try {
const data = await this.fetchData(this.getUrl());
if (!data || !data.currently || typeof data.currently.temperature === "undefined") {
throw new Error("No usable data received from Pirate Weather API.");
}
const currentWeather = this.generateWeatherDayFromCurrentWeather(data);
this.setCurrentWeather(currentWeather);
})
.catch(function (request) {
Log.error("[weatherprovider.pirateweather] Could not load data ... ", request);
})
.finally(() => this.updateAvailable());
const currentWeather = this.generateWeatherDayFromCurrentWeather(data);
this.setCurrentWeather(currentWeather);
} catch (error) {
Log.error("Could not load data ... ", error);
} finally {
this.updateAvailable();
}
},
fetchWeatherForecast () {
this.fetchData(this.getUrl())
.then((data) => {
if (!data || !data.daily || !data.daily.data.length) {
// No usable data?
return;
}
async fetchWeatherForecast () {
try {
const data = await this.fetchData(this.getUrl());
if (!data || !data.daily || !data.daily.data.length) {
throw new Error("No usable data received from Pirate Weather API.");
}
const forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
this.setWeatherForecast(forecast);
})
.catch(function (request) {
Log.error("[weatherprovider.pirateweather] Could not load data ... ", request);
})
.finally(() => this.updateAvailable());
const forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
this.setWeatherForecast(forecast);
} catch (error) {
Log.error("Could not load data ... ", error);
} finally {
this.updateAvailable();
}
},
// Create a URL from the config and base URL.