Skip to content

Commit

Permalink
Fix error handling of HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fboes committed Jun 18, 2019
1 parent 362ae35 commit be08db9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change log
==========

1.2.7
-----

* 💊 Fix error handling of HTTP requests

1.2.6
-----

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

For more information about command line options, call the command line variant of this tool with `--help` appended to it.

There is also a [source code documentation on command-line parameters](https://github.com/fboes/aerofly-wettergeraet/blob/master/src/WettergeraetLib/Argumentor.cpp#L20) applicable for the command-line as well as the desktop variant of this tool.
There is also a [source code documentation on command-line parameters](https://github.com/fboes/aerofly-wettergeraet/blob/master/src/WettergeraetLib/Argumentor.cpp#L65) applicable for the command-line as well as the desktop variant of this tool.

To append parameters to the desktop application, right-click your desktop icon, select "Properties" and append the parameter(s) to the shortcut's target.

Expand Down
16 changes: 14 additions & 2 deletions src/WettergeraetLib/FetchUrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ std::string FetchUrl::fetch(std::string url, unsigned short fetchMode, std::stri
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); //s

// Do request
res = curl_easy_perform(curl);
Expand All @@ -75,10 +76,21 @@ std::string FetchUrl::fetch(std::string url, unsigned short fetchMode, std::stri
if (res == CURLE_HTTP_RETURNED_ERROR) {
throw std::invalid_argument("Invalid HTTP status code returned for " + url);
}
else if (res == CURLE_OPERATION_TIMEDOUT) {
throw std::invalid_argument("Timeout for " + url);
}
else if (res != CURLE_OK) {
throw std::invalid_argument(curl_easy_strerror(res));
}

if (buffer != "") {
return (fetchMode == FetchUrl::MODE_JSON) ? this->parseJson(buffer) : buffer;
if (fetchMode == FetchUrl::MODE_JSON) {
buffer = this->parseJson(buffer);
}
if (buffer == "") {
throw std::invalid_argument("Empty or invalid response returned for " + url);
}

return buffer;
}

throw std::invalid_argument("Could no initiate CURL");
Expand Down

0 comments on commit be08db9

Please sign in to comment.