diff --git a/app/error/middleware/log-to-console.js b/app/error/middleware/log-to-console.js index 9cf7c86..fa8c363 100644 --- a/app/error/middleware/log-to-console.js +++ b/app/error/middleware/log-to-console.js @@ -11,31 +11,34 @@ let ValidationError = require('../type/client/validation'); */ module.exports = function(error, req, res, next) { - //Log validation errors differently - if (error instanceof ValidationError && error.data && error.data.fields) { - console.log(chalk.red( - error.name + (error.message ? (': ' + error.message) : '') - )); - let fields = error.data.fields; - for (let field in fields) { - if (fields.hasOwnProperty(field)) { - console.log(chalk.red(' - ', field + ':', fields[field].message)); - } - } - } - //Log stack if present - else if (error.stack) { + if (error.stack) { console.log(chalk.red(error.stack)); } - //Log error name and code + //Log error name and message else { console.log(chalk.red( error.name + (error.message ? (': ' + error.message) : '') )); } + //Log validation errors fields + if (error instanceof ValidationError && error.data && error.data.fields) { + let fields = error.data.fields; + let lines = []; + for (let field in fields) { + if (fields.hasOwnProperty(field)) { + let message = fields[field].message || fields[field].type; + lines.push(chalk.red(' - ', field + ':', message)); + } + } + if (lines.length) { + console.log(chalk.red('\nFields:')); + lines.forEach(line => console.log(line)); + } + } + //Call next middleware next(error); }; diff --git a/app/utils/validation.js b/app/utils/validation.js index 1b2a189..52213a4 100644 --- a/app/utils/validation.js +++ b/app/utils/validation.js @@ -8,24 +8,24 @@ module.exports = { /** * Create field error helper for use in data validation promise chains */ - fieldError(field, type) { - return {field, type}; + fieldError(field, type, message) { + return {field, type, message}; }, /** * Find field errors helper for use in promise chains */ - findFieldErrors(results) { - let errors = {}; + getErroredFields(results) { + let fields = {}; results.forEach(result => { if (result && result.field && result.type) { - let field = result[0]; - let type = result[1]; - errors[field] = {type}; + let {field, type, message} = result; + fields[field] = {type, message}; } }); - if (Object.keys(errors).length > 0) { - throw errors; + if (Object.keys(fields).length > 0) { + return fields; } + return null; } };