Skip to content

Commit

Permalink
Adding more details to error messaging. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
mac- committed Oct 2, 2014
1 parent 4662432 commit ea41b9b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/RequestValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,37 @@ var RequestValidator = function(plugin, options) {
},

onPreHandler = function(request, next) {
var report, errorMessages;
var report, errorMessages,
extractErrorMessage = function(error) {
var message = (error.description) ? error.message + '. Description: ' + error.description : error.message;
return message;
};

report = routeSchemaManager.validatePath(request);
if (!report.valid) {
errorMessages = 'path parameters validation error: ' +
_.map(report.errors, function(error) { return error.message; }).join(', ');
_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
return next(plugin.hapi.error.badRequest(errorMessages));
}

report = routeSchemaManager.validateQuery(request);
if (!report.valid) {
errorMessages = 'query parameters validation error: ' +
_.map(report.errors, function(error) { return error.message; }).join(', ');
_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
return next(plugin.hapi.error.badRequest(errorMessages));
}

report = routeSchemaManager.validateHeaders(request);
if (!report.valid) {
errorMessages = 'header parameters validation error: ' +
_.map(report.errors, function(error) { return error.message; }).join(', ');
_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
return next(plugin.hapi.error.badRequest(errorMessages));
}

report = routeSchemaManager.validatePayload(request);
if (!report.valid) {
errorMessages = 'payload parameters validation error: ' +
_.map(report.errors, function(error) { return error.message; }).join(', ');
_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
return next(plugin.hapi.error.badRequest(errorMessages));
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"contributors": [
"Mac Angell <mac.ang311@gmail.com>"
],
"version": "0.6.1",
"version": "0.6.2",
"dependencies": {
"underscore": "1.x.x",
"hoek": "2.x.x",
Expand Down

0 comments on commit ea41b9b

Please sign in to comment.