From 93151a3b0caffd2ae6d6c77675466892889e8f3f Mon Sep 17 00:00:00 2001 From: J Guthrie Date: Sat, 3 Jun 2017 18:40:47 +0100 Subject: [PATCH] Add GoogleMapsService::Error::NotFoundError - When doing a directions search, if you enter an invalid address (i.e. 'dsadsfdfdscfrcerf, Tanzania'), a NotFoundError is raised --- lib/google_maps_service/apis/directions.rb | 2 +- lib/google_maps_service/client.rb | 4 +++- lib/google_maps_service/errors.rb | 6 +++++- spec/google_maps_service/apis/directions_spec.rb | 14 +++++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/google_maps_service/apis/directions.rb b/lib/google_maps_service/apis/directions.rb index 6ae9fa2..87ee3af 100644 --- a/lib/google_maps_service/apis/directions.rb +++ b/lib/google_maps_service/apis/directions.rb @@ -98,4 +98,4 @@ def directions(origin, destination, return get('/maps/api/directions/json', params)[:routes] end end -end \ No newline at end of file +end diff --git a/lib/google_maps_service/client.rb b/lib/google_maps_service/client.rb index 4c98c89..efecaa9 100644 --- a/lib/google_maps_service/client.rb +++ b/lib/google_maps_service/client.rb @@ -281,9 +281,11 @@ def check_body_error(response, body) raise GoogleMapsService::Error::RequestDeniedError.new(response), body[:error_message] when 'INVALID_REQUEST' raise GoogleMapsService::Error::InvalidRequestError.new(response), body[:error_message] + when 'NOT_FOUND' + raise GoogleMapsService::Error::NotFoundError.new(response), (body[:error_message] || 'ADDRESS NOT FOUND') else raise GoogleMapsService::Error::ApiError.new(response), body[:error_message] end end end -end \ No newline at end of file +end diff --git a/lib/google_maps_service/errors.rb b/lib/google_maps_service/errors.rb index 642ba0b..582bb7e 100644 --- a/lib/google_maps_service/errors.rb +++ b/lib/google_maps_service/errors.rb @@ -48,5 +48,9 @@ class RateLimitError < ApiError # An unathorized error occurred. It might be caused by invalid key/secret or invalid access. class RequestDeniedError < ApiError end + + # When an Address is not found. i.e. An address string could not be geocoded + class NotFoundError < ApiError + end end -end \ No newline at end of file +end diff --git a/spec/google_maps_service/apis/directions_spec.rb b/spec/google_maps_service/apis/directions_spec.rb index 7cebeb0..c45f9f7 100644 --- a/spec/google_maps_service/apis/directions_spec.rb +++ b/spec/google_maps_service/apis/directions_spec.rb @@ -71,6 +71,18 @@ end end + context 'unparseable / non-existing address' do + before(:example) do + stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/directions\/.*/) + .to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: '{"results": [], "status": "NOT_FOUND"}') + end + + it 'should raise GoogleMapsService::Error::NotFoundError' do + expect { client.directions('dsfdsfdsfdsfdsfadsfdrcgtzvhstrvzstrhstrgcrscgtr', 'Sydney Town Hall', + mode: 'driving') }.to raise_error GoogleMapsService::Error::NotFoundError + end + end + context 'travel mode round trip' do it 'should call Google Maps Web Service' do routes = client.directions('Town Hall, Sydney', 'Parramatta, NSW', @@ -146,4 +158,4 @@ expect(a_request(:get, 'https://maps.googleapis.com/maps/api/directions/json?origin=Sydney+Town+Hall&destination=Parramatta+Town+Hall&alternatives=true&key=%s' % api_key)).to have_been_made end end -end \ No newline at end of file +end