Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(errors): convert missing parameter errors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
philbooth committed Jul 17, 2015
1 parent eec574c commit 2bbdc7e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var inherits = require('util').inherits
var messages = require('joi/lib/language').errors

var DEFAULTS = {
code: 500,
Expand Down Expand Up @@ -75,7 +76,11 @@ AppError.translate = function (response) {
}
}
else if (payload.validation) {
error = AppError.invalidRequestParameter(payload.validation)
if (payload.message && payload.message.indexOf(messages.any.required) >= 0) {
error = AppError.missingRequestParameter(payload.validation.keys[0])
} else {
error = AppError.invalidRequestParameter(payload.validation)
}
}
else if (payload.statusCode === 400 && TOO_LARGE.test(payload.message)) {
error = AppError.requestBodyTooLarge()
Expand Down
79 changes: 79 additions & 0 deletions test/local/error_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var test = require('../ptaptest')
var messages = require('joi/lib/language')
var AppError = require('../../lib/error')

test(
'tightly-coupled joi message hack is okay',
function (t) {
t.equal(typeof messages.errors.any.required, 'string')
t.not(messages.errors.any.required, '')
t.end()
}
)

test(
'exported functions exist',
function (t) {
t.equal(typeof AppError, 'function')
t.equal(AppError.length, 3)
t.equal(typeof AppError.translate, 'function')
t.equal(AppError.translate.length, 1)
t.equal(typeof AppError.invalidRequestParameter, 'function')
t.equal(AppError.invalidRequestParameter.length, 1)
t.equal(typeof AppError.missingRequestParameter, 'function')
t.equal(AppError.missingRequestParameter.length, 1)
t.end()
}
)

test(
'error.translate with missing required parameters',
function (t) {
var result = AppError.translate({
output: {
payload: {
message: 'foo' + messages.errors.any.required,
validation: {
keys: [ 'bar', 'baz' ]
}
}
}
})
t.ok(result instanceof AppError, 'instanceof AppError')
t.equal(result.errno, 108)
t.equal(result.message, 'Missing parameter in request body: bar')
t.equal(result.output.statusCode, 400)
t.equal(result.output.payload.error, 'Bad Request')
t.equal(result.output.payload.errno, result.errno)
t.equal(result.output.payload.message, result.message)
t.equal(result.output.payload.param, 'bar')
t.end()
}
)

test(
'error.translate with invalid parameter',
function (t) {
var result = AppError.translate({
output: {
payload: {
validation: 'foo'
}
}
})
t.ok(result instanceof AppError, 'instanceof AppError')
t.equal(result.errno, 107)
t.equal(result.message, 'Invalid parameter in request body')
t.equal(result.output.statusCode, 400)
t.equal(result.output.payload.error, 'Bad Request')
t.equal(result.output.payload.errno, result.errno)
t.equal(result.output.payload.message, result.message)
t.equal(result.output.payload.validation, 'foo')
t.end()
}
)

0 comments on commit 2bbdc7e

Please sign in to comment.