Merge pull request #12 from strongloop/array-no-status-code

Remove statusCode from details in Array errors
This commit is contained in:
David Cheung 2016-05-25 13:51:08 -04:00
commit 57343bba20
3 changed files with 29 additions and 20 deletions

18
lib/clone.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = cloneAllProperties;
/**
* clone the error properties to the data objects
* [err.name, err.message, err.stack] are not enumerable properties
* @param data Object to be altered
* @param err Error Object
*/
function cloneAllProperties(data, err) {
data.name = err.name;
data.message = err.message;
for (var p in err) {
if ((p in data)) continue;
data[p] = err[p];
}
// stack is appended last to ensure order is the same for response
data.stack = err.stack;
};

View File

@ -5,6 +5,7 @@
'use strict';
var cloneAllProperties = require('../lib/clone.js');
var httpStatus = require('http-status');
module.exports = function buildResponseData(err, isDebugMode) {
@ -44,6 +45,7 @@ function serializeArrayOfErrors(errors) {
var data = {};
cloneAllProperties(data, err);
delete data.statusCode;
details.push(data);
}
@ -74,16 +76,3 @@ 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

@ -5,6 +5,7 @@
'use strict';
var cloneAllProperties = require('../lib/clone.js');
var debug = require('debug')('test');
var expect = require('chai').expect;
var http = require('http');
@ -313,10 +314,14 @@ describe('strong-error-handler', function() {
var data = res.body.error;
expect(data).to.have.property('message').that.match(/multiple errors/);
var expectTestError = getExpectedErrorData(testError);
delete expectTestError.statusCode;
var expectAnotherError = getExpectedErrorData(anotherError);
delete expectAnotherError.statusCode;
var expectedDetails = [
getExpectedErrorData(testError),
getExpectedErrorData(anotherError),
expectTestError,
expectAnotherError,
'ERR STRING',
];
expect(data).to.have.property('details').to.eql(expectedDetails);
@ -432,10 +437,7 @@ function ErrorWithProps(props) {
util.inherits(ErrorWithProps, Error);
function getExpectedErrorData(err) {
// "stack" is a non-enumerable property
var data = {stack: err.stack};
for (var prop in err) {
data[prop] = err[prop];
}
var data = {};
cloneAllProperties(data, err);
return data;
}