aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-05-02 19:27:39 +0200
committerTeddy Wing2018-05-02 19:27:39 +0200
commitd53978539c71d598d7cb5f47ad9e3f30a83eb06a (patch)
treebfaad8de0ff179d56f7f0c3c7f8a5f7d1decbb2f
parentb6c0477552576e7f8575bf2dca6e7899b640c012 (diff)
downloadchouette-core-d53978539c71d598d7cb5f47ad9e3f30a83eb06a.tar.bz2
RouteWayCostCalculator: Don't update `costs` if response errors
If there's an API error, we shouldn't update the route's `costs` field. Let's say we've already calculated some costs for a route A. We then edit and re-save A, which triggers a recalculation of the costs. Now the TomTom API responds with an error. We don't want to overwrite our existing costs with an empty array because they could still be useful. In this case, we should instead keep the existing costs we already had. To achieve this, move the `RemoteError` rescue into `RouteWayCostCalculator`, leaving the error unhandled in `TomTom.matrix`. Refs #6884
-rw-r--r--app/services/route_way_cost_calculator.rb2
-rw-r--r--lib/tom_tom/matrix.rb8
-rw-r--r--spec/services/route_way_cost_calculator_spec.rb25
3 files changed, 31 insertions, 4 deletions
diff --git a/app/services/route_way_cost_calculator.rb b/app/services/route_way_cost_calculator.rb
index d41a2e59a..58f9ad1d0 100644
--- a/app/services/route_way_cost_calculator.rb
+++ b/app/services/route_way_cost_calculator.rb
@@ -8,5 +8,7 @@ class RouteWayCostCalculator
way_costs = TomTom.matrix(way_costs)
way_costs = WayCostCollectionJSONSerializer.dump(way_costs)
@route.update(costs: way_costs)
+ rescue TomTom::Matrix::RemoteError => e
+ Rails.logger.error "TomTom::Matrix server error: #{e}"
end
end
diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb
index b57495f3f..75c2cc02b 100644
--- a/lib/tom_tom/matrix.rb
+++ b/lib/tom_tom/matrix.rb
@@ -4,6 +4,10 @@ module TomTom
@connection = connection
end
+ # Exceptions:
+ #
+ # * This raises a `TomTom::Matrix::RemoteError` when the API responds with
+ # an error.
def matrix(way_costs)
points_with_ids = points_from_way_costs(way_costs)
points = points_as_params(points_with_ids)
@@ -29,10 +33,6 @@ module TomTom
points_with_ids,
matrix_json
)
- rescue RemoteError => e
- Rails.logger.error "TomTom::Matrix server error: #{e}"
-
- []
end
def points_from_way_costs(way_costs)
diff --git a/spec/services/route_way_cost_calculator_spec.rb b/spec/services/route_way_cost_calculator_spec.rb
index 79b81e34d..593ba29ef 100644
--- a/spec/services/route_way_cost_calculator_spec.rb
+++ b/spec/services/route_way_cost_calculator_spec.rb
@@ -29,5 +29,30 @@ RSpec.describe RouteWayCostCalculator do
expect(route.costs).not_to be_nil
expect { JSON.parse(JSON.dump(route.costs)) }.not_to raise_error
end
+
+ it "doesn't update route costs when there is a server error" do
+ route = create(:route)
+
+ stub_request(
+ :post,
+ "https://api.tomtom.com/routing/1/matrix/json?key&routeType=shortest&travelMode=bus"
+ )
+ .with(
+ headers: {
+ 'Accept'=>'*/*',
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
+ 'Content-Type'=>'application/json',
+ 'User-Agent'=>'Faraday v0.9.2'
+ })
+ .to_return(
+ status: 200,
+ body: "{\"formatVersion\":\"0.0.1\",\"error\":{\"description\":\"Outputformat:csvisunsupported.\"}}",
+ headers: {}
+ )
+
+ RouteWayCostCalculator.new(route).calculate!
+
+ expect(route.costs).to be_nil
+ end
end
end