nchaulet / node-geocoder

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

Throw on 402 error #145

Open mlucool opened 8 years ago

mlucool commented 8 years ago

Hi,

When I go over my API limit for opencage it throws: "Error: Response status code is 402" from \lib\httpadapter\httpadapter.js:58:25

This is acceptable, but there is no way to catch it. When I go over the Google API limit, the error it is thrown in a catchable manner and I see output: "Error: Error: Status is OVER_QUERY_LIMIT. You have exceeded your rate-limit for this API."

Below is an example to reproduce these two cases. Google is how it should work, while opencage I believe has a bug.

import 'babel-polyfill';

import co from 'co';
import NodeGeocoder from 'node-geocoder';
const geocoder = NodeGeocoder('opencage', 'https', {apiKey: 'MYAPYKEYHERE'});
//const geocoder = NodeGeocoder('google');

co(function *() {
    for (let i = 0; i < 5000; ++i) {
        try {
            const res = yield geocoder.reverse({lat: '40.71', lon: '-74.0'});
            console.log(res)
        } catch (err) {
            console.log('Error: ' + err);
        }
    }
});
nchaulet commented 8 years ago

I thinks we could have a generic QuotaExcedeed error.

mlucool commented 8 years ago

That would be helpful. A starting set of generic errors could include: QuotaExceeded, RateExceeced, InvalidAPIKey, InvalidRequest, ServerError