2019-05-09 12:51:57 +00:00
|
|
|
// Copyright IBM Corp. 2016,2018. All Rights Reserved.
|
2016-05-11 11:53:28 +00:00
|
|
|
// Node module: strong-error-handler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-10-11 13:57:15 +00:00
|
|
|
const cloneAllProperties = require('../lib/clone.js');
|
|
|
|
const httpStatus = require('http-status');
|
2016-05-11 11:53:28 +00:00
|
|
|
|
2018-05-31 17:13:14 +00:00
|
|
|
module.exports = buildResponseData;
|
|
|
|
|
|
|
|
function buildResponseData(err, options) {
|
2017-01-30 03:59:35 +00:00
|
|
|
// Debugging mode is disabled by default. When turned on (in dev),
|
|
|
|
// all error properties (including) stack traces are sent in the response
|
2018-05-31 17:13:14 +00:00
|
|
|
const isDebugMode = options.debug;
|
2017-01-30 03:59:35 +00:00
|
|
|
|
2018-05-31 17:13:14 +00:00
|
|
|
if (Array.isArray(err)) {
|
|
|
|
return serializeArrayOfErrors(err, options);
|
2016-05-11 11:53:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 17:13:14 +00:00
|
|
|
const data = Object.create(null);
|
2016-05-11 11:53:28 +00:00
|
|
|
fillStatusCode(data, err);
|
|
|
|
|
|
|
|
if (typeof err !== 'object') {
|
2016-05-16 14:57:15 +00:00
|
|
|
err = {
|
|
|
|
statusCode: 500,
|
|
|
|
message: '' + err,
|
|
|
|
};
|
2016-05-11 11:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isDebugMode) {
|
|
|
|
fillDebugData(data, err);
|
2018-05-31 17:13:14 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.statusCode >= 400 && data.statusCode <= 499) {
|
2016-05-11 11:53:28 +00:00
|
|
|
fillBadRequestError(data, err);
|
|
|
|
} else {
|
|
|
|
fillInternalError(data, err);
|
|
|
|
}
|
|
|
|
|
2018-05-31 17:13:14 +00:00
|
|
|
const safeFields = options.safeFields || [];
|
2017-01-30 13:48:38 +00:00
|
|
|
fillSafeFields(data, err, safeFields);
|
2017-01-30 03:59:35 +00:00
|
|
|
|
2016-05-11 11:53:28 +00:00
|
|
|
return data;
|
2019-10-11 13:57:15 +00:00
|
|
|
}
|
2016-05-11 11:53:28 +00:00
|
|
|
|
2018-05-31 17:13:14 +00:00
|
|
|
function serializeArrayOfErrors(errors, options) {
|
|
|
|
const details = errors.map(e => buildResponseData(e, options));
|
2016-05-11 11:53:28 +00:00
|
|
|
return {
|
2018-05-31 17:13:14 +00:00
|
|
|
statusCode: 500,
|
2016-05-11 11:53:28 +00:00
|
|
|
message: 'Failed with multiple errors, ' +
|
|
|
|
'see `details` for more information.',
|
|
|
|
details: details,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function fillStatusCode(data, err) {
|
|
|
|
data.statusCode = err.statusCode || err.status;
|
|
|
|
if (!data.statusCode || data.statusCode < 400)
|
|
|
|
data.statusCode = 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fillDebugData(data, err) {
|
2016-05-16 14:57:15 +00:00
|
|
|
cloneAllProperties(data, err);
|
2016-05-11 11:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fillBadRequestError(data, err) {
|
|
|
|
data.name = err.name;
|
|
|
|
data.message = err.message;
|
2017-01-30 10:44:35 +00:00
|
|
|
data.code = err.code;
|
2016-05-11 11:53:28 +00:00
|
|
|
data.details = err.details;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fillInternalError(data, err) {
|
|
|
|
data.message = httpStatus[data.statusCode] || 'Unknown Error';
|
|
|
|
}
|
2017-01-30 03:59:35 +00:00
|
|
|
|
|
|
|
function fillSafeFields(data, err, safeFields) {
|
|
|
|
if (!Array.isArray(safeFields)) {
|
|
|
|
safeFields = [safeFields];
|
|
|
|
}
|
|
|
|
|
|
|
|
safeFields.forEach(function(field) {
|
2018-03-05 03:39:51 +00:00
|
|
|
if (err[field] !== undefined) {
|
|
|
|
data[field] = err[field];
|
|
|
|
}
|
2017-01-30 03:59:35 +00:00
|
|
|
});
|
|
|
|
}
|