diff options
| -rw-r--r-- | lib/tom_tom/matrix.rb | 19 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix_spec.rb | 29 |
2 files changed, 47 insertions, 1 deletions
diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index b0c8cc335..b57495f3f 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -20,11 +20,19 @@ module TomTom req.body = build_request_body(points) end + matrix_json = JSON.parse(response.body) + + check_for_error_response(matrix_json) + extract_costs_to_way_costs!( way_costs, points_with_ids, - JSON.parse(response.body) + matrix_json ) + rescue RemoteError => e + Rails.logger.error "TomTom::Matrix server error: #{e}" + + [] end def points_from_way_costs(way_costs) @@ -76,6 +84,12 @@ module TomTom }) end + def check_for_error_response(matrix_json) + if matrix_json.has_key?('error') + raise RemoteError, matrix_json['error']['description'] + end + end + def extract_costs_to_way_costs!(way_costs, points, matrix_json) way_costs = [] @@ -110,5 +124,8 @@ module TomTom way_costs end + + + class RemoteError < RuntimeError; end end end diff --git a/spec/lib/tom_tom/matrix_spec.rb b/spec/lib/tom_tom/matrix_spec.rb index 605f1d254..be63447d4 100644 --- a/spec/lib/tom_tom/matrix_spec.rb +++ b/spec/lib/tom_tom/matrix_spec.rb @@ -149,6 +149,35 @@ RSpec.describe TomTom::Matrix do end end + describe "#check_for_error_response" do + it "raises an RemoteError when an 'error' key is present in the response" do + response_body = { + 'formatVersion' => '0.0.1', + 'error' => { + 'description' => 'Output format: csv is unsupported.' + } + } + + expect { + matrix.check_for_error_response(response_body) + }.to raise_error( + TomTom::Matrix::RemoteError, + 'Output format: csv is unsupported.' + ) + end + + it "doesn't raise errors with a normal response" do + response_body = { + 'formatVersion' => '0.0.1', + 'matrix' => [] + } + + expect { + matrix.check_for_error_response(response_body) + }.to_not raise_error + end + end + describe "#extract_costs_to_way_costs!" do it "puts distance & time costs in way_costs" do way_costs = [ |
