Skip to content

add error handling to weather fetch functions, including cors #3791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ planned for 2025-07-01
- [calendar] fix fullday event rrule until with timezone offset (#3781)
- [feat] Add rule `no-undef` in config file validation to fix #3785 (#3786)
- [fonts] Fix `roboto.css` to avoid error message `Unknown descriptor 'var(' in @font-face rule.` in firefox console (#3787)
- [weather] add error handling to fetch functions including cors

### Updated

Expand Down
20 changes: 12 additions & 8 deletions js/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,29 @@ async function cors (req, res) {
url = `invalid url: ${req.url}`;
Log.error(url);
res.send(url);
} else {
url = match[1];

const headersToSend = getHeadersToSend(req.url);
const expectedReceivedHeaders = geExpectedReceivedHeaders(req.url);
return;
}
url = match[1];

Log.log(`cors url: ${url}`);
const response = await fetch(url, { headers: headersToSend });
const headersToSend = getHeadersToSend(req.url);
const expectedReceivedHeaders = geExpectedReceivedHeaders(req.url);
Log.log(`cors url: ${url}`);

const response = await fetch(url, { headers: headersToSend });
if (response.ok) {
for (const header of expectedReceivedHeaders) {
const headerValue = response.headers.get(header);
if (header) res.set(header, headerValue);
}
const data = await response.text();
res.send(data);
} else {
res.status(response.status).json({ message: response.statusText });
}

} catch (error) {
Log.error(error);
res.send(error);
res.status(500).json({ message: error.message });
}
}

Expand Down
24 changes: 15 additions & 9 deletions modules/default/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ 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 (response.ok) {
const data = await response.text();

if (type === "xml") {
return new DOMParser().parseFromString(data, "text/html");
} else {
if (!data || !data.length > 0) return undefined;
if (type === "xml") {
return new DOMParser().parseFromString(data, "text/html");
} else {
if (!data || !data.length > 0) return "null"; //undefined;

const dataResponse = JSON.parse(data);
if (!dataResponse.headers) {
dataResponse.headers = getHeadersFromResponse(expectedResponseHeaders, response);
const dataResponse = JSON.parse(data);
if (!dataResponse.headers) {
dataResponse.headers = getHeadersFromResponse(expectedResponseHeaders, response);
}
return dataResponse;
}
return dataResponse;
} else {
throw new Error(`Response status: ${response.status}`);
}

}

/**
Expand Down
Loading