Commit 5e9d872 1 parent 8b09bf3 commit 5e9d872 Copy full SHA for 5e9d872
File tree 2 files changed +34
-4
lines changed
2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -122,11 +122,11 @@ function formatError(req, h) {
122
122
if ( response . output . statusCode === 500 ) {
123
123
req . log ( 'error' , response . data instanceof Buffer ? response . data . toString ( ) : response . data ) ;
124
124
}
125
+ }
125
126
126
- if ( response . data . isJoi && response . data . details && typeof response . data . details . map === 'function' ) {
127
- const message = response . data . details . map ( getJoiErrorMessage ) . join ( ', ' ) ;
128
- errorObject . details = message ;
129
- }
127
+ if ( response . isJoi && response . details && typeof response . details . map === 'function' ) {
128
+ const message = response . details . map ( getJoiErrorMessage ) . join ( ', ' ) ;
129
+ errorObject . details = message ;
130
130
}
131
131
132
132
return h . response ( {
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const Hapi = require ( 'hapi' ) ;
4
+ const Joi = require ( 'joi' ) ;
4
5
const assert = require ( 'chai' ) . assert ;
5
6
const jsonapi = require ( '../' ) ;
6
7
const plugin = jsonapi . plugin ;
7
8
8
9
describe ( 'JSON API Plugin' , ( ) => {
9
10
11
+ describe ( 'Error formatting' , ( ) => {
12
+ it ( 'should format joi errors to more human readable output' , async ( ) => {
13
+ const server = new Hapi . Server ( { debug : { request : '*' } } ) ;
14
+ await server . register ( { plugin} ) ;
15
+ server . route ( {
16
+ method : 'POST' ,
17
+ path : '/test' ,
18
+ config : {
19
+ validate : {
20
+ failAction : ( req , h , err ) => err ,
21
+ payload : {
22
+ fo : Joi . object ( {
23
+ bar : Joi . object ( {
24
+ banana : Joi . number ( ) . required ( )
25
+ } ) . required ( )
26
+ } ) . required ( )
27
+ }
28
+ } ,
29
+ handler ( req , h ) {
30
+ return h . response ( ) . code ( 204 ) ;
31
+ }
32
+ }
33
+ } ) ;
34
+
35
+ const res = await server . inject ( { method : 'POST' , url : '/test' , payload : { fo : { } } } ) ;
36
+ assert . equal ( res . result . errors [ 0 ] . details , 'Validation error: "bar" is required (fo,bar)' ) ;
37
+ } ) ;
38
+ } ) ;
39
+
10
40
describe ( 'Decorated reply functions' , ( ) => {
11
41
let server ;
12
42
You can’t perform that action at this time.
0 commit comments