alexreisner / geocoder

Complete Ruby geocoding solution.
http://www.rubygeocoder.com
MIT License
6.32k stars 1.19k forks source link

Error when MapBox auth key is invalid #1657

Open maxolasersquad opened 3 weeks ago

maxolasersquad commented 3 weeks ago

Expected behavior

No error should be thrown.

Actual behavior

The following error is produced:

/usr/local/bundle/gems/geocoder-1.8.3/lib/geocoder/lookups/base.rb:46:in `search': undefined method `map' for false:FalseClass (NoMethodError)

        results(query).map{ |r|
                      ^^^^
Did you mean?  tap

Steps to reproduce

Set up a Ruby on Rails application with Geocoder and configure it for MapBox, but with an invalid auth token. Try to save an Address model that uses Geocoder.

irb(main):001:0> a = Address.find 47
  Address Load (0.6ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" = $1 LIMIT $2  [["id", 47], ["LIMIT", 1]]
=> 
#<Address:0x0000741c81b8f2b0
...
> a.save
  TRANSACTION (0.2ms)  BEGIN
Dalli::Server#connect 170.23.0.7:11211
Geocoding API error: 401 Unauthorized
  TRANSACTION (0.3ms)  ROLLBACK
/usr/local/bundle/gems/geocoder-1.8.3/lib/geocoder/lookups/base.rb:46:in `search': undefined method `map' for false:FalseClass (NoMethodError)

        results(query).map{ |r|
                      ^^^^
Did you mean?  tap

Environment info

More information

Geocoder configuration.

Geocoder.configure(
  lookup: :mapbox,
  use_https: true,
  api_key: ENV['MAPBOX'],
  units: :mi,
)
alexreisner commented 3 weeks ago

Thanks for this. I agree that it should be handled more gracefully. A Geocoder::InvalidApiKey exception should be raised (if exception-raising is configured) or an empty array should be returned. Looks like the test that checks this isn't written properly. I'm open to a pull request.