nchaulet / node-geocoder

nodejs geocoding library
http://nchaulet.github.io/node-geocoder/
MIT License
926 stars 213 forks source link

Crash with HereGeocoder when bad apiKey given #313

Closed magiksd closed 2 years ago

magiksd commented 3 years ago

Hello, when you give a bad apiKey to the Here geocoder, the Node.js app will crash because response is not correctly handled.

The Here API will return the following JSON when no apiKey given : {"error":"Unauthorized","error_description":"No credentials found"}

...and when bad apiKey is given : {"error":"Unauthorized","error_description":"apiKey invalid. apiKey not found."}

In both cases, it will crash the app with this error : TypeError: Cannot read property 'View' of undefined

It's because lib/geocoder/heregeocoder.js is not handling theses errors in the httpAdapter response, the code tries to access directly to result.Response.View[0] without checking if result.error is present or even if result.Response and result.Response.View exists.

It will be fine if I could catch the error to fail silently and prevent my app to crash, but I can't and I don't really know why because it's possible with Google geocoder when giving bad apiKey. With Here geocoder, it ignore my try/catch block and crash the app, so I can't use it in production safely if my users setup a bad apiKey.

nchaulet commented 2 years ago

Resolved by #342