File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed
Expand file tree Collapse file tree 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) {
122122 if ( response . output . statusCode === 500 ) {
123123 req . log ( 'error' , response . data instanceof Buffer ? response . data . toString ( ) : response . data ) ;
124124 }
125+ }
125126
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 ;
130130 }
131131
132132 return h . response ( {
Original file line number Diff line number Diff line change 11'use strict' ;
22
33const Hapi = require ( 'hapi' ) ;
4+ const Joi = require ( 'joi' ) ;
45const assert = require ( 'chai' ) . assert ;
56const jsonapi = require ( '../' ) ;
67const plugin = jsonapi . plugin ;
78
89describe ( 'JSON API Plugin' , ( ) => {
910
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+
1040 describe ( 'Decorated reply functions' , ( ) => {
1141 let server ;
1242
You can’t perform that action at this time.
0 commit comments