Skip to content

Commit

Permalink
Catch exception when trying to connect to OSRM
Browse files Browse the repository at this point in the history
This allow to have a better error message

Tested by setting the --osrmHost param to the IPv6 addr (::1)
  • Loading branch information
greenscientist committed Mar 12, 2024
1 parent dbf18dd commit d6aa035
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/osrmgeofilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,26 @@ namespace TrRouting {
queryString += "&sources=0";
}

using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
HttpClient client(host + ":" + port);
auto s = client.request("GET", queryString);

if (s->status_code != "200 OK") {
spdlog::error("Error fetching OSRM data ({})", s->status_code);
//TODO We should throw an exception somehow here to invalidate the current calculation
// and returne an informative error code to the user
std::stringstream responseJsonSs;
try {
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
HttpClient client(host + ":" + port);
auto s = client.request("GET", queryString);

if (s->status_code != "200 OK") {
spdlog::error("Error fetching OSRM data ({})", s->status_code);
//TODO We should throw an exception somehow here to invalidate the current calculation
// and returne an informative error code to the user
return accessibleNodesFootpaths;
}

responseJsonSs << s->content.rdbuf();
} catch (const std::exception& e){
spdlog::error("exception during OSRM request: {}", e.what());
//TODO See above TODO about handling the errors
return accessibleNodesFootpaths;
}

std::stringstream responseJsonSs;
responseJsonSs << s->content.rdbuf();
nlohmann::json responseJson = nlohmann::json::parse(responseJsonSs.str());

if (responseJson["durations"] != nullptr && responseJson["distances"] != nullptr && responseJson["durations"][0] != nullptr && responseJson["distances"][0] != nullptr)
Expand Down

0 comments on commit d6aa035

Please sign in to comment.