@@ -2,12 +2,15 @@ import express from 'express';
2
2
import { Server as HttpServer } from 'http' ;
3
3
import HttpStatusCodes from 'http-status-codes' ;
4
4
import { Get } from '../../src/decorator/Get' ;
5
+ import { Post } from '../../src/decorator/Post' ;
5
6
import { JsonController } from '../../src/decorator/JsonController' ;
6
7
import { Middleware } from '../../src/decorator/Middleware' ;
7
8
import { UseAfter } from '../../src/decorator/UseAfter' ;
9
+ import { BodyParam } from '../../src/decorator/BodyParam' ;
8
10
import { ExpressErrorMiddlewareInterface } from '../../src/driver/express/ExpressErrorMiddlewareInterface' ;
9
11
import { HttpError } from '../../src/http-error/HttpError' ;
10
12
import { NotFoundError } from '../../src/http-error/NotFoundError' ;
13
+ import { UnprocessableEntityError } from '../../src/http-error/UnprocessableEntityError' ;
11
14
import { createExpressServer , getMetadataArgsStorage } from '../../src/index' ;
12
15
import { axios } from '../utilities/axios' ;
13
16
import DoneCallback = jest . DoneCallback ;
@@ -111,6 +114,11 @@ describe(``, () => {
111
114
stories ( ) : never {
112
115
throw new ToJsonError ( 503 , 'sorry, try it again later' , 'impatient user' ) ;
113
116
}
117
+
118
+ @Post ( '/videos' )
119
+ createVideo ( @BodyParam ( 'meta' ) meta : string ) : never {
120
+ throw new UnprocessableEntityError ( 'Meta is an array of strings.' ) ;
121
+ }
114
122
}
115
123
116
124
expressServer = createExpressServer ( ) . listen ( 3001 , done ) ;
@@ -182,5 +190,17 @@ describe(``, () => {
182
190
expect ( error . response . data . secretData ) . toBeUndefined ( ) ;
183
191
}
184
192
} ) ;
193
+
194
+ it ( 'should call global error handler middleware for validation params' , async ( ) => {
195
+ expect . assertions ( 3 ) ;
196
+
197
+ try {
198
+ await axios . post ( '/videos' , { meta : 'new' } ) ;
199
+ } catch ( error ) {
200
+ expect ( errorHandlerCalled ) . toBeTruthy ( ) ;
201
+ expect ( error . response . status ) . toEqual ( HttpStatusCodes . UNPROCESSABLE_ENTITY ) ;
202
+ expect ( error . response . data . message ) . toEqual ( 'Meta is an array of strings.' ) ;
203
+ }
204
+ } ) ;
185
205
} ) ;
186
206
} ) ;
0 commit comments