Merge pull request #11 from strongloop/debug-error-message

Include err.message for debug data
This commit is contained in:
David Cheung 2016-05-20 11:30:16 -04:00
commit ec04268074
2 changed files with 45 additions and 16 deletions

View File

@ -16,9 +16,10 @@ module.exports = function buildResponseData(err, isDebugMode) {
fillStatusCode(data, err);
if (typeof err !== 'object') {
data.statusCode = 500;
data.message = '' + err;
err = {};
err = {
statusCode: 500,
message: '' + err,
};
}
if (isDebugMode) {
@ -41,10 +42,8 @@ function serializeArrayOfErrors(errors) {
continue;
}
var data = {stack: err.stack};
for (var p in err) { // eslint-disable-line one-var
data[p] = err[p];
}
var data = {};
cloneAllProperties(data, err);
details.push(data);
}
@ -63,12 +62,7 @@ function fillStatusCode(data, err) {
}
function fillDebugData(data, err) {
for (var p in err) {
if ((p in data)) continue;
data[p] = err[p];
}
// NOTE err.stack is not an enumerable property
data.stack = err.stack;
cloneAllProperties(data, err);
}
function fillBadRequestError(data, err) {
@ -80,3 +74,16 @@ function fillBadRequestError(data, err) {
function fillInternalError(data, err) {
data.message = httpStatus[data.statusCode] || 'Unknown Error';
}
function cloneAllProperties(data, err) {
// NOTE err.name and err.message are not enumerable properties
data.name = err.name;
data.message = err.message;
for (var p in err) {
if ((p in data)) continue;
data[p] = err[p];
}
// NOTE err.stack is not an enumerable property
// stack is appended last to ensure order is the same for response
data.stack = err.stack;
}

View File

@ -191,6 +191,7 @@ describe('strong-error-handler', function() {
context('JSON response', function() {
it('contains all error properties when debug=true', function(done) {
var error = new ErrorWithProps({
message: 'a test error message',
details: 'some details',
extra: 'sensitive data',
});
@ -199,15 +200,36 @@ describe('strong-error-handler', function() {
requestJson().end(function(err, res) {
if (err) return done(err);
var expectedData = {statusCode: 500, stack: error.stack};
for (var key in error) expectedData[key] = error[key];
var expectedData = {
statusCode: 500,
message: 'a test error message',
name: 'ErrorWithProps',
details: 'some details',
extra: 'sensitive data',
stack: error.stack,
};
expect(res.body).to.have.property('error');
expect(res.body.error).to.eql(expectedData);
done();
});
});
it('contains non-enumerable Error properties when debug=true',
function(done) {
var error = new Error('a test error message');
givenErrorHandlerForError(error, {debug: true});
requestJson().end(function(err, res) {
if (err) return done(err);
expect(res.body).to.have.property('error');
var resError = res.body.error;
expect(resError).to.have.property('name', 'Error');
expect(resError).to.have.property('message',
'a test error message');
expect(resError).to.have.property('stack', error.stack);
done();
});
});
it('contains subset of properties when status=4xx', function(done) {
var error = new ErrorWithProps({
name: 'ValidationError',