@@ -2,12 +2,15 @@ import express from 'express';
22import { Server as HttpServer } from 'http' ;
33import HttpStatusCodes from 'http-status-codes' ;
44import { Get } from '../../src/decorator/Get' ;
5+ import { Post } from '../../src/decorator/Post' ;
56import { JsonController } from '../../src/decorator/JsonController' ;
67import { Middleware } from '../../src/decorator/Middleware' ;
78import { UseAfter } from '../../src/decorator/UseAfter' ;
9+ import { BodyParam } from '../../src/decorator/BodyParam' ;
810import { ExpressErrorMiddlewareInterface } from '../../src/driver/express/ExpressErrorMiddlewareInterface' ;
911import { HttpError } from '../../src/http-error/HttpError' ;
1012import { NotFoundError } from '../../src/http-error/NotFoundError' ;
13+ import { UnprocessableEntityError } from '../../src/http-error/UnprocessableEntityError' ;
1114import { createExpressServer , getMetadataArgsStorage } from '../../src/index' ;
1215import { axios } from '../utilities/axios' ;
1316import DoneCallback = jest . DoneCallback ;
@@ -111,6 +114,11 @@ describe(``, () => {
111114 stories ( ) : never {
112115 throw new ToJsonError ( 503 , 'sorry, try it again later' , 'impatient user' ) ;
113116 }
117+
118+ @Post ( '/videos' )
119+ createVideo ( @BodyParam ( 'meta' ) meta : string ) : never {
120+ throw new UnprocessableEntityError ( 'Meta is an array of strings.' ) ;
121+ }
114122 }
115123
116124 expressServer = createExpressServer ( ) . listen ( 3001 , done ) ;
@@ -182,5 +190,17 @@ describe(``, () => {
182190 expect ( error . response . data . secretData ) . toBeUndefined ( ) ;
183191 }
184192 } ) ;
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+ } ) ;
185205 } ) ;
186206} ) ;
0 commit comments