Skip to content

Commit

Permalink
error santization
Browse files Browse the repository at this point in the history
  • Loading branch information
rclark committed Jul 30, 2015
1 parent 21b0ea5 commit d663812
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
25 changes: 25 additions & 0 deletions lib/format_error.js
@@ -0,0 +1,25 @@
'use strict';

function stripToken(str) {
if (typeof str !== 'string') return str;
return str.replace(/access_token=[^& ]+/, 'access_token=...');
}

/**
* Sanitize superagent error objects
*
* @private
* @param {object} err an error object received from a superagent HTTP request
* @returns {object} a sanitized representation of the HTTP error
*/
module.exports = function(err) {
if (!err) return null;
if (!err.response || !err.response.error) return err;
var error = err.response.error;
if (typeof error !== 'object') return err;
error.message = stripToken(error.message || 'Unknown Error');
Object.keys(error).forEach(function(key) {
error[key] = stripToken(error[key]);
});
return error;
};
30 changes: 20 additions & 10 deletions lib/services/datasets.js
Expand Up @@ -5,6 +5,7 @@ var invariant = require('invariant'),
request = require('superagent'),
hat = require('hat'),
makeService = require('../make_service'),
formatError = require('../format_error'),
constants = require('../constants'),
makeURL = require('../make_url');

Expand Down Expand Up @@ -46,7 +47,8 @@ Datasets.prototype.listDatasets = function(callback) {
var url = makeURL(this, constants.API_DATASET_DATASETS, { owner: this.user });

request(url, function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -89,7 +91,8 @@ Datasets.prototype.createDataset = function(options, callback) {
.post(url)
.send(options)
.end(function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -124,7 +127,8 @@ Datasets.prototype.readDataset = function(dataset, callback) {
});

request(url, function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -168,7 +172,8 @@ Datasets.prototype.updateDataset = function(dataset, options, callback) {
.patch(url)
.send(options)
.end(function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -197,7 +202,7 @@ Datasets.prototype.deleteDataset = function(dataset, callback) {
request
.del(url)
.end(function(err) {
callback(err);
callback(formatError(err));
});
};

Expand Down Expand Up @@ -241,7 +246,8 @@ Datasets.prototype.listFeatures = function(dataset, callback) {
});

request(url, function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -335,7 +341,8 @@ Datasets.prototype.insertFeature = function(feature, dataset, callback) {
.put(url)
.send(feature)
.end(function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -376,7 +383,8 @@ Datasets.prototype.readFeature = function(id, dataset, callback) {
});

request(url, function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -408,7 +416,8 @@ Datasets.prototype.deleteFeature = function(id, dataset, callback) {
request
.del(url)
.end(function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};

Expand Down Expand Up @@ -520,6 +529,7 @@ Datasets.prototype.bulkFeatureUpdate = function(update, dataset, callback) {
.post(url)
.send({ put: inserts, delete: deletes })
.end(function(err, res) {
callback(err, res.body);
if (err) return callback(formatError(err));
callback(null, res.body);
});
};
3 changes: 2 additions & 1 deletion lib/services/directions.js
Expand Up @@ -3,6 +3,7 @@
var invariant = require('invariant'),
request = require('superagent'),
formatPoints = require('../format_points'),
formatError = require('../format_error'),
makeService = require('../make_service'),
makeURL = require('../make_url'),
constants = require('../constants');
Expand Down Expand Up @@ -108,7 +109,7 @@ MapboxDirections.prototype.getDirections = function(waypoints, options, callback
});

request(url, function(err, res) {
callback(err, res.body);
callback(formatError(err), res.body);
});
};

Expand Down
7 changes: 4 additions & 3 deletions lib/services/geocoder.js
Expand Up @@ -4,7 +4,8 @@ var invariant = require('invariant'),
request = require('superagent'),
makeService = require('../make_service'),
makeURL = require('../make_url'),
constants = require('../constants');
constants = require('../constants'),
formatError = require('../format_error');

var MapboxGeocoder = makeService('MapboxGeocoder');

Expand Down Expand Up @@ -71,7 +72,7 @@ MapboxGeocoder.prototype.geocodeForward = function(query, options, callback) {
}, queryOptions);

request(url, function(err, res) {
callback(err, res.body);
callback(formatError(err), res.body);
});
};

Expand Down Expand Up @@ -127,7 +128,7 @@ MapboxGeocoder.prototype.geocodeReverse = function(location, options, callback)
}, {});

request(url, function(err, res) {
callback(err, res.body);
callback(formatError(err), res.body);
});
};

Expand Down
3 changes: 2 additions & 1 deletion lib/services/matching.js
Expand Up @@ -5,6 +5,7 @@ var invariant = require('invariant'),
geojsonhint = require('geojsonhint/object'),
makeService = require('../make_service'),
makeURL = require('../make_url'),
formatError = require('../format_error'),
constants = require('../constants');

var MapboxMatching = makeService('MapboxMatching');
Expand Down Expand Up @@ -110,7 +111,7 @@ MapboxMatching.prototype.matching = function(trace, options, callback) {
request.post(url)
.send(trace)
.end(function(err, res) {
callback(err, res.body);
callback(formatError(err), res.body);
});
};

Expand Down
3 changes: 2 additions & 1 deletion lib/services/surface.js
Expand Up @@ -3,6 +3,7 @@
var invariant = require('invariant'),
request = require('superagent'),
formatPoints = require('../format_points'),
formatError = require('../format_error'),
makeService = require('../make_service'),
makeURL = require('../make_url'),
constants = require('../constants');
Expand Down Expand Up @@ -92,7 +93,7 @@ MapboxSurface.prototype.surface = function(mapid, layer, fields, path, options,
}, surfaceOptions);

request(url, function(err, res) {
callback(err, res.body);
callback(formatError(err), res.body);
});
};

Expand Down
6 changes: 3 additions & 3 deletions test/datasets.js
Expand Up @@ -118,7 +118,7 @@ test('DatasetClient', function(datasetClient) {
assert.ok(client, 'created dataset client');
client.readDataset('peanuts', function(err, response) {
assert.equal(err.status, 422, 'expected HTTP error');
assert.equal(response.message, 'No dataset', 'expected message');
assert.notOk(response, 'no response');
assert.end();
});
});
Expand Down Expand Up @@ -231,7 +231,7 @@ test('DatasetClient', function(datasetClient) {
assert.ok(client, 'created dataset client');
client.readFeature('club', testDatasets[0], function(err, response) {
assert.equal(err.status, 404, 'expected error code');
assert.equal(response.message, 'Feature does not exist', 'expected error message');
assert.notOk(response, 'no response');
assert.end();
});
});
Expand Down Expand Up @@ -298,7 +298,7 @@ test('DatasetClient', function(datasetClient) {
assert.ok(client, 'created dataset client');
client.deleteFeature('blt', testDatasets[0], function(err, response) {
assert.equal(err.status, 404, 'expected error code');
assert.equal(response.message, 'Feature does not exist', 'expected error message');
assert.notOk(response, 'no response');
assert.end();
});
});
Expand Down

0 comments on commit d663812

Please sign in to comment.