Remove statusCode from details in Array errors

This is to preserve behavior from strong-remoting rest adapter
This commit is contained in:
David Cheung 2016-05-20 19:15:14 -04:00
parent 6fb9f941b4
commit 9f74606847
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'; 'use strict';
var cloneAllProperties = require('../lib/clone.js');
var httpStatus = require('http-status'); var httpStatus = require('http-status');
module.exports = function buildResponseData(err, isDebugMode) { module.exports = function buildResponseData(err, isDebugMode) {
@ -44,6 +45,7 @@ function serializeArrayOfErrors(errors) {
var data = {}; var data = {};
cloneAllProperties(data, err); cloneAllProperties(data, err);
delete data.statusCode;
details.push(data); details.push(data);
} }
@ -74,16 +76,3 @@ function fillBadRequestError(data, err) {
function fillInternalError(data, err) { function fillInternalError(data, err) {
data.message = httpStatus[data.statusCode] || 'Unknown Error'; 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'; 'use strict';
var cloneAllProperties = require('../lib/clone.js');
var debug = require('debug')('test'); var debug = require('debug')('test');
var expect = require('chai').expect; var expect = require('chai').expect;
var http = require('http'); var http = require('http');
@ -313,10 +314,14 @@ describe('strong-error-handler', function() {
var data = res.body.error; var data = res.body.error;
expect(data).to.have.property('message').that.match(/multiple errors/); 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 = [ var expectedDetails = [
getExpectedErrorData(testError), expectTestError,
getExpectedErrorData(anotherError), expectAnotherError,
'ERR STRING', 'ERR STRING',
]; ];
expect(data).to.have.property('details').to.eql(expectedDetails); expect(data).to.have.property('details').to.eql(expectedDetails);
@ -432,10 +437,7 @@ function ErrorWithProps(props) {
util.inherits(ErrorWithProps, Error); util.inherits(ErrorWithProps, Error);
function getExpectedErrorData(err) { function getExpectedErrorData(err) {
// "stack" is a non-enumerable property var data = {};
var data = {stack: err.stack}; cloneAllProperties(data, err);
for (var prop in err) {
data[prop] = err[prop];
}
return data; return data;
} }