Skip to content

Commit faf414c

Browse files
committed
Improve HTTP error handling
1 parent 38db362 commit faf414c

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/updateinformation/GithubReleasesZsyncUpdateInformation.h

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,37 @@ namespace appimage::update::updateinformation {
4545

4646
std::string builtUrl;
4747

48-
// continue only if HTTP status is good
49-
if (response.status_code >= 200 && response.status_code < 300) {
50-
// in contrary to the original implementation, instead of converting wildcards into
51-
// all-matching regular expressions, we have the power of fnmatch() available, a real wildcard
52-
// implementation
53-
// unfortunately, this is still hoping for GitHub's JSON API to return a pretty printed
54-
// response which can be parsed like this
55-
std::stringstream responseText(response.text);
56-
std::string currentLine;
57-
58-
// not ideal, but allows for returning a match for the entire line
59-
auto pattern = "*" + filename + "*";
60-
61-
// iterate through all lines to find a possible download URL and compare it to the pattern
62-
while (std::getline(responseText, currentLine)) {
63-
if (currentLine.find("browser_download_url") != std::string::npos) {
64-
downloadUrlLines++;
65-
if (fnmatch(pattern.c_str(), currentLine.c_str(), 0) == 0) {
66-
matchingUrls++;
67-
auto parts = util::split(currentLine, '"');
68-
builtUrl = std::string(parts.back());
69-
break;
70-
}
48+
// continue only if request worked
49+
if (response.error.code != cpr::ErrorCode::OK || response.status_code < 200 || response.status_code >= 300) {
50+
std::ostringstream oss;
51+
oss << "GitHub API request failed: HTTP status " << std::to_string(response.status_code)
52+
<< ", CURL error: " << response.error.message;
53+
throw UpdateInformationError(oss.str());
54+
}
55+
56+
57+
// in contrary to the original implementation, instead of converting wildcards into
58+
// all-matching regular expressions, we have the power of fnmatch() available, a real wildcard
59+
// implementation
60+
// unfortunately, this is still hoping for GitHub's JSON API to return a pretty printed
61+
// response which can be parsed like this
62+
std::stringstream responseText(response.text);
63+
std::string currentLine;
64+
65+
// not ideal, but allows for returning a match for the entire line
66+
auto pattern = "*" + filename + "*";
67+
68+
// iterate through all lines to find a possible download URL and compare it to the pattern
69+
while (std::getline(responseText, currentLine)) {
70+
if (currentLine.find("browser_download_url") != std::string::npos) {
71+
downloadUrlLines++;
72+
if (fnmatch(pattern.c_str(), currentLine.c_str(), 0) == 0) {
73+
matchingUrls++;
74+
auto parts = util::split(currentLine, '"');
75+
builtUrl = std::string(parts.back());
76+
break;
7177
}
7278
}
73-
} else {
74-
throw UpdateInformationError("GitHub API request failed!");
7579
}
7680

7781
if (downloadUrlLines <= 0) {

0 commit comments

Comments
 (0)