Trasladar al cliente más detalles sobre los errores

This commit is contained in:
David Arranz 2019-07-09 12:43:29 +02:00
parent 9a279c966f
commit a0ec336125

View File

@ -2,12 +2,27 @@
const _ = require('lodash'); const _ = require('lodash');
const httpStatus = require('http-status'); const httpStatus = require('http-status');
const Sequelize = require('sequelize');
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS // PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
function buildErrorLog(err) { function buildErrorLog(err) {
if (err instanceof Sequelize.ValidationError) {
return {
message: err.message,
httpCode: httpStatus.UNPROCESSABLE_ENTITY, // <- datos no válidos
code: (err.parent) ? err.parent.code : '',
payload: err.fields,
}
}
return buildErrorLogOriginal(err);
}
function buildErrorLogOriginal(err) {
let errorLog; let errorLog;
if (_.isUndefined(err)) { if (_.isUndefined(err)) {
errorLog = 'Error not defined'; errorLog = 'Error not defined';
@ -26,9 +41,11 @@ function buildErrorResponse(nameController, nameMethod, error) {
const errorDescription = buildErrorLog(error); const errorDescription = buildErrorLog(error);
const jsonResultFailed = { const jsonResultFailed = {
code: httpStatus.INTERNAL_SERVER_ERROR, statusCode: errorDescription.httpCode ? errorDescription.httpCode : httpStatus.INTERNAL_SERVER_ERROR,
message: 'Internal Server Error', message: errorDescription.message ? errorDescription.message : 'Internal Server Error',
description: `Internal Application Error in ${nameController}:${nameMethod}. ${errorDescription}` code: errorDescription.code ? errorDescription.code : 'Undefined',
description: `Internal Application Error in ${nameController}:${nameMethod}.`,
payload: errorDescription
} }
return jsonResultFailed; return jsonResultFailed;
} }
@ -108,7 +125,7 @@ function extractParamsFromRequest(req, res, extraParams = {}) {
function handleErrorResponse(controllerName, methodName, error, res) { function handleErrorResponse(controllerName, methodName, error, res) {
const jsonResultFailed = buildErrorResponse(controllerName, methodName, error); const jsonResultFailed = buildErrorResponse(controllerName, methodName, error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(jsonResultFailed); res.status(jsonResultFailed.statusCode).send(jsonResultFailed);
} }
function handleResultResponse(result, totalCount = null, params, res, statusCode = httpStatus.OK) { function handleResultResponse(result, totalCount = null, params, res, statusCode = httpStatus.OK) {