Merge pull request #453 from CartoDB/stringify-for-error-log
Stringify for error log
This commit is contained in:
commit
49183b8802
@ -64,9 +64,29 @@ function setErrorHeader(err, statusCode, res) {
|
||||
|
||||
errorsLog.statusCode = statusCode || 200;
|
||||
errorsLog.message = errorsLog.error[0];
|
||||
// remove double quotes for logs
|
||||
errorsLog.message = errorsLog.message.replace(/"/g, "");
|
||||
delete errorsLog.error;
|
||||
|
||||
res.set('X-SQLAPI-Errors', JSON.stringify(errorsLog));
|
||||
res.set('X-SQLAPI-Errors', stringifyForLogs(errorsLog));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove problematic nested characters
|
||||
* from object for logs RegEx
|
||||
*
|
||||
* @param {Object} object
|
||||
*/
|
||||
function stringifyForLogs(object) {
|
||||
Object.keys(object).map(key => {
|
||||
if(typeof object[key] === 'string') {
|
||||
object[key] = object[key].replace(/[^a-zA-Z0-9]/g, ' ');
|
||||
} else if (typeof object[key] === 'object') {
|
||||
stringifyForLogs(object[key]);
|
||||
} else if (object[key] instanceof Array) {
|
||||
for (let element of object[key]) {
|
||||
stringifyForLogs(element);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return JSON.stringify(object);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ var errorHandler = require('../../app/utils/error_handler');
|
||||
|
||||
describe('error-handler', function() {
|
||||
it('should return a header with errors', function (done) {
|
||||
const error = new Error('error test');
|
||||
let error = new Error('error test');
|
||||
error.detail = 'test detail';
|
||||
error.hint = 'test hint';
|
||||
error.context = 'test context';
|
||||
@ -46,7 +46,7 @@ describe('error-handler', function() {
|
||||
});
|
||||
|
||||
it('JSONP should return a header with error statuscode', function (done) {
|
||||
const error = new Error('error test');
|
||||
let error = new Error('error test');
|
||||
error.detail = 'test detail';
|
||||
error.hint = 'test hint';
|
||||
error.context = 'test context';
|
||||
@ -87,5 +87,51 @@ describe('error-handler', function() {
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('should escape chars that broke logs regex', function (done) {
|
||||
const badString = 'error: ( ) = " \" \' * $ & |';
|
||||
const escapedString = 'error ';
|
||||
|
||||
let error = new Error(badString);
|
||||
error.detail = badString;
|
||||
error.hint = badString;
|
||||
error.context = badString;
|
||||
|
||||
const res = {
|
||||
req: {
|
||||
query: { callback: true }
|
||||
},
|
||||
headers: {},
|
||||
set (key, value) {
|
||||
this.headers[key] = value;
|
||||
},
|
||||
header (key, value) {
|
||||
this.set(key, value);
|
||||
},
|
||||
statusCode: 0,
|
||||
status (status) {
|
||||
this.statusCode = status;
|
||||
},
|
||||
jsonp () {}
|
||||
};
|
||||
|
||||
const errorHeader = {
|
||||
detail: escapedString,
|
||||
hint: escapedString,
|
||||
context: escapedString,
|
||||
statusCode: 400,
|
||||
message: escapedString
|
||||
};
|
||||
|
||||
errorHandler(error, res);
|
||||
|
||||
assert.ok(res.headers['X-SQLAPI-Errors'].length > 0);
|
||||
assert.deepEqual(
|
||||
res.headers['X-SQLAPI-Errors'],
|
||||
JSON.stringify(errorHeader)
|
||||
);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user