From aab6fdeafff4783ddb40897228415dd211248b80 Mon Sep 17 00:00:00 2001 From: dmitrii Date: Wed, 10 Apr 2024 23:17:12 +0200 Subject: [PATCH 1/6] Check routing response body parsing result --- src/routing/http_wrapper.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp index 63847232b..1811d1e07 100644 --- a/src/routing/http_wrapper.cpp +++ b/src/routing/http_wrapper.cpp @@ -141,11 +141,10 @@ std::string HttpWrapper::run_query(const std::string& query) const { void HttpWrapper::parse_response(rapidjson::Document& json_result, const std::string& json_content) { -#ifdef NDEBUG json_result.Parse(json_content.c_str()); -#else - assert(!json_result.Parse(json_content.c_str()).HasParseError()); -#endif + if (json_result.HasParseError()) { + throw RoutingException("Failed to parse routing response body: " + json_content); + } } Matrices HttpWrapper::get_matrices(const std::vector& locs) const { From 104355cf903f289e2857ec246c5160a154ff8feb Mon Sep 17 00:00:00 2001 From: dmitrii Date: Sat, 13 Apr 2024 19:08:06 +0200 Subject: [PATCH 2/6] Fix ors response checking for cases where HTTP error is returned in request body --- src/routing/ors_wrapper.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/routing/ors_wrapper.cpp b/src/routing/ors_wrapper.cpp index 56f1ac193..1272567b9 100644 --- a/src/routing/ors_wrapper.cpp +++ b/src/routing/ors_wrapper.cpp @@ -65,8 +65,19 @@ void OrsWrapper::check_response(const rapidjson::Document& json_result, const std::vector&, const std::string&) const { if (json_result.HasMember("error")) { - throw RoutingException( - std::string(json_result["error"]["message"].GetString())); + if (json_result["error"].IsString()) { + if (json_result.HasMember("path") && json_result["path"].IsString()) { + throw RoutingException(std::string(json_result["error"].GetString()) + + " " + + std::string(json_result["path"].GetString())); + } + throw RoutingException(std::string(json_result["error"].GetString())); + } + if (json_result["error"].HasMember("message") && + json_result["error"]["message"].IsString()) { + throw RoutingException( + std::string(json_result["error"]["message"].GetString())); + } } } From 3d209f96f091a3fbee6c4d6e7b984de948b36d4b Mon Sep 17 00:00:00 2001 From: dmitrii Date: Sat, 13 Apr 2024 19:10:00 +0200 Subject: [PATCH 3/6] Apply code formatting to http_wrapper --- src/routing/http_wrapper.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp index 1811d1e07..178eb7dd0 100644 --- a/src/routing/http_wrapper.cpp +++ b/src/routing/http_wrapper.cpp @@ -143,7 +143,8 @@ void HttpWrapper::parse_response(rapidjson::Document& json_result, const std::string& json_content) { json_result.Parse(json_content.c_str()); if (json_result.HasParseError()) { - throw RoutingException("Failed to parse routing response body: " + json_content); + throw RoutingException("Failed to parse routing response body: " + + json_content); } } From b8e8b989d5819a01fea9695a82419b73a4c2bb23 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Wed, 10 Jul 2024 17:26:32 +0200 Subject: [PATCH 4/6] Do not include full response in error in case of parsing failure. --- src/routing/http_wrapper.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp index 178eb7dd0..e53ab3e0c 100644 --- a/src/routing/http_wrapper.cpp +++ b/src/routing/http_wrapper.cpp @@ -143,8 +143,7 @@ void HttpWrapper::parse_response(rapidjson::Document& json_result, const std::string& json_content) { json_result.Parse(json_content.c_str()); if (json_result.HasParseError()) { - throw RoutingException("Failed to parse routing response body: " + - json_content); + throw RoutingException("Failed to parse routing response."); } } From 128a1a5160b2030b6cff36d4f5e438c25a49c852 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Wed, 10 Jul 2024 17:40:31 +0200 Subject: [PATCH 5/6] Small restructure for error handling. --- src/routing/ors_wrapper.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/routing/ors_wrapper.cpp b/src/routing/ors_wrapper.cpp index 1272567b9..7221c80cc 100644 --- a/src/routing/ors_wrapper.cpp +++ b/src/routing/ors_wrapper.cpp @@ -65,18 +65,22 @@ void OrsWrapper::check_response(const rapidjson::Document& json_result, const std::vector&, const std::string&) const { if (json_result.HasMember("error")) { - if (json_result["error"].IsString()) { - if (json_result.HasMember("path") && json_result["path"].IsString()) { - throw RoutingException(std::string(json_result["error"].GetString()) + - " " + - std::string(json_result["path"].GetString())); - } - throw RoutingException(std::string(json_result["error"].GetString())); - } - if (json_result["error"].HasMember("message") && + if (json_result["error"].IsObject() && + json_result["error"].HasMember("message") && json_result["error"]["message"].IsString()) { + // Normal ORS error syntax. throw RoutingException( std::string(json_result["error"]["message"].GetString())); + } else { + // Web framework error uses another convention, see #1083. + if (json_result["error"].IsString()) { + auto error = std::string(json_result["error"].GetString()); + + if (json_result.HasMember("path") && json_result["path"].IsString()) { + error += " " + std::string(json_result["path"].GetString()); + } + throw RoutingException(error); + } } } } From f251a6f4e90f845239f69e4b5fb297e189f69361 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Wed, 10 Jul 2024 17:49:21 +0200 Subject: [PATCH 6/6] Update changelog. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceedfb155..f4555abd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### Fixed +- Routing errors handling with ORS (#1083) + ## [v1.14.0] - 2024-01-16 ### Added