Merge pull request #816 from CartoDB/stringify-for-error-log
Stringify for error log
This commit is contained in:
commit
a40bc4a527
@ -187,5 +187,27 @@ function setErrorHeader(errors, statusCode, res) {
|
||||
};
|
||||
});
|
||||
|
||||
res.set('X-Tiler-Errors', JSON.stringify(errorsLog));
|
||||
res.set('X-Tiler-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);
|
||||
}
|
||||
|
@ -122,4 +122,57 @@ describe('error-middleware', function() {
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('should escape chars that broke logs regex', function (done) {
|
||||
const badString = 'error: ( ) = " \" \' * $ & |';
|
||||
const escapedString = 'error ';
|
||||
|
||||
const error = new Error(badString);
|
||||
error.label = badString;
|
||||
error.type = badString;
|
||||
error.subtype = badString;
|
||||
|
||||
const errors = [error, error];
|
||||
|
||||
const req = {};
|
||||
const res = {
|
||||
headers: {},
|
||||
set (key, value) {
|
||||
this.headers[key] = value;
|
||||
},
|
||||
statusCode: 0,
|
||||
status (status) {
|
||||
this.statusCode = status;
|
||||
},
|
||||
json () {},
|
||||
send () {}
|
||||
};
|
||||
|
||||
const errorHeader = {
|
||||
mainError: {
|
||||
statusCode: 400,
|
||||
message: escapedString,
|
||||
name: error.name,
|
||||
label: escapedString,
|
||||
type: escapedString,
|
||||
subtype: escapedString,
|
||||
},
|
||||
moreErrors: [{
|
||||
message: escapedString,
|
||||
name: error.name,
|
||||
label: escapedString,
|
||||
type: escapedString,
|
||||
subtype: escapedString
|
||||
}]
|
||||
};
|
||||
|
||||
const errorFn = errorMiddleware();
|
||||
errorFn(errors, req, res);
|
||||
|
||||
assert.deepEqual(res.headers, {
|
||||
'X-Tiler-Errors': JSON.stringify(errorHeader)
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user