Skip to content

Commit

Permalink
Merge pull request #32 from ubilabs/master
Browse files Browse the repository at this point in the history
Return original error when the request went wrong.
  • Loading branch information
Fabrizio Moscon committed Sep 13, 2014
2 parents 09fab36 + 82ec6bd commit 267f6da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/googlemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ exports.checkAndConvertPoint = function(input) {
var returnObjectFromJSON = function(callback) {
if (typeof callback === 'function') {
return function(err, jsonString) {

if (err){
callback(err);
return;
}

try {
callback(err, JSON.parse(jsonString));
} catch (e) {
Expand Down Expand Up @@ -525,7 +531,9 @@ var makeRequest = function(path, args, secure, callback, encoding) {
if (res.statusCode === 200) {
return callback(null, data);
}
return callback(new Error("Response status code: " + res.statusCode), data);
error = new Error(data);
error.code = res.statusCode;
return callback(error, data);
});
}

Expand Down
42 changes: 42 additions & 0 deletions test/errors-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var vows = require('vows'),
assert = require('assert'),
gm = require('../lib/googlemaps');

vows.describe('errors').addBatch({
'No connection': {
topic: function(options) {
gm.config('proxy', 'http://no-valid-proxy.com');
gm.geocode('Hamburg', this.callback);

// reset the proxy
gm.config('proxy', null);
},
'returns an error': function(err, result) {
assert.isUndefined(result);
assert.isObject(err);
},
'returns the error code ENOTFOUND': function(err, result) {
assert.equal(err.code, 'ENOTFOUND');
}
},

'Wrong Credentials': {
topic: function(options) {
gm.config('google-client-id', 'clientID');
gm.config('google-private-key', 'WRONG-KEY');
gm.geocode('Hamburg', this.callback);

// reset credentials
gm.config('google-client-id', null);
gm.config('google-private-key', null);
},
'returns the expected street view URL': function(err, data) {
assert.isUndefined(data);
assert.isObject(err);
},
'returns status 403 - Unable to authenticate': function(err, data) {
assert.equal(err.code, 403);
assert.include(err.message, 'Unable to authenticate');
}
}
}).export(module);

0 comments on commit 267f6da

Please sign in to comment.