Skip to content

Commit 4799b46

Browse files
authored
Incorrect error message when unable to snap all input coordinates (#5846)
In cases where we are unable to find a phantom node for an input coordinate, we return an error indicating which coordinate failed. This would always refer to the coordinate with index equal to the number of valid phantom nodes found. We fix this by instead returning the first index for which a phantom node could not be found.
1 parent f6b313e commit 4799b46

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- CHANGED: Reduce memory usage for raster source handling. [#5572](https://github.com/Project-OSRM/osrm-backend/pull/5572)
2222
- CHANGED: Add cmake option `ENABLE_DEBUG_LOGGING` to control whether output debug logging. [#3427](https://github.com/Project-OSRM/osrm-backend/issues/3427)
2323
- CHANGED: updated extent of Hong Kong as left hand drive country. [#5535](https://github.com/Project-OSRM/osrm-backend/issues/5535)
24+
- FIXED: corrected error message when failing to snap input coordinates [#5846](https://github.com/Project-OSRM/osrm-backend/pull/5846)
2425
- Infrastructure
2526
- REMOVED: STXXL support removed as STXXL became abandonware. [#5760](https://github.com/Project-OSRM/osrm-backend/pull/5760)
2627
# 5.21.0

include/engine/plugins/plugin_base.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,22 @@ class BasePlugin
371371
}
372372
return phantom_node_pairs;
373373
}
374+
375+
std::string MissingPhantomErrorMessage(const std::vector<PhantomNodePair> &phantom_nodes,
376+
const std::vector<util::Coordinate> &coordinates) const
377+
{
378+
BOOST_ASSERT(phantom_nodes.size() < coordinates.size());
379+
auto mismatch = std::mismatch(phantom_nodes.begin(),
380+
phantom_nodes.end(),
381+
coordinates.begin(),
382+
coordinates.end(),
383+
[](const auto &phantom_node, const auto &coordinate) {
384+
return phantom_node.first.input_location == coordinate;
385+
});
386+
std::size_t missing_index = std::distance(phantom_nodes.begin(), mismatch.first);
387+
return std::string("Could not find a matching segment for coordinate ") +
388+
std::to_string(missing_index);
389+
}
374390
};
375391
}
376392
}

src/engine/plugins/table.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
7575

7676
if (phantom_nodes.size() != params.coordinates.size())
7777
{
78-
return Error("NoSegment",
79-
std::string("Could not find a matching segment for coordinate ") +
80-
std::to_string(phantom_nodes.size()),
81-
result);
78+
return Error(
79+
"NoSegment", MissingPhantomErrorMessage(phantom_nodes, params.coordinates), result);
8280
}
8381

8482
auto snapped_phantoms = SnapPhantomNodes(phantom_nodes);

src/engine/plugins/trip.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ Status TripPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
199199
if (phantom_node_pairs.size() != number_of_locations)
200200
{
201201
return Error("NoSegment",
202-
std::string("Could not find a matching segment for coordinate ") +
203-
std::to_string(phantom_node_pairs.size()),
202+
MissingPhantomErrorMessage(phantom_node_pairs, parameters.coordinates),
204203
result);
205204
}
206205
BOOST_ASSERT(phantom_node_pairs.size() == number_of_locations);

src/engine/plugins/viaroute.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ Status ViaRoutePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithm
9090
if (phantom_node_pairs.size() != route_parameters.coordinates.size())
9191
{
9292
return Error("NoSegment",
93-
std::string("Could not find a matching segment for coordinate ") +
94-
std::to_string(phantom_node_pairs.size()),
93+
MissingPhantomErrorMessage(phantom_node_pairs, route_parameters.coordinates),
9594
result);
9695
}
9796
BOOST_ASSERT(phantom_node_pairs.size() == route_parameters.coordinates.size());

unit_tests/library/table.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ BOOST_AUTO_TEST_CASE(test_table_no_segment_for_some_coordinates)
242242
BOOST_CHECK(rc == Status::Error);
243243
const auto code = json_result.values.at("code").get<json::String>().value;
244244
BOOST_CHECK_EQUAL(code, "NoSegment");
245+
const auto message = json_result.values.at("message").get<json::String>().value;
246+
BOOST_CHECK_EQUAL(message, "Could not find a matching segment for coordinate 0");
245247
}
246248

247249
BOOST_AUTO_TEST_CASE(test_table_serialiaze_fb)

0 commit comments

Comments
 (0)