@@ -17,7 +17,7 @@ import APIError from '../server/helpers/APIError';
1717const app = express ( ) ;
1818
1919if ( config . env === 'development' ) {
20- app . use ( logger ( 'dev' ) ) ;
20+ app . use ( logger ( 'dev' ) ) ;
2121}
2222
2323// parse body params and attache them to req.body
@@ -36,52 +36,52 @@ app.use(cors());
3636
3737// enable detailed API logging in dev env
3838if ( config . env === 'development' ) {
39- expressWinston . requestWhitelist . push ( 'body' ) ;
40- expressWinston . responseWhitelist . push ( 'body' ) ;
41- app . use ( expressWinston . logger ( {
42- winstonInstance,
43- meta : true , // optional: log meta data about request (defaults to true)
44- msg : 'HTTP {{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms' ,
45- colorStatus : true // Color the status code (default green, 3XX cyan, 4XX yellow, 5XX red).
46- } ) ) ;
39+ expressWinston . requestWhitelist . push ( 'body' ) ;
40+ expressWinston . responseWhitelist . push ( 'body' ) ;
41+ app . use ( expressWinston . logger ( {
42+ winstonInstance,
43+ meta : true , // optional: log meta data about request (defaults to true)
44+ msg : 'HTTP {{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms' ,
45+ colorStatus : true // Color the status code (default green, 3XX cyan, 4XX yellow, 5XX red).
46+ } ) ) ;
4747}
4848
4949// mount all routes on /api path
5050app . use ( '/api' , routes ) ;
5151
5252// if error is not an instanceOf APIError, convert it.
5353app . use ( ( err , req , res , next ) => {
54- if ( err instanceof expressValidation . ValidationError ) {
55- // validation error contains errors which is an array of error each containing message[]
56- const unifiedErrorMessage = err . errors . map ( error => error . messages . join ( '. ' ) ) . join ( ' and ' ) ;
57- const error = new APIError ( unifiedErrorMessage , err . status , true ) ;
58- return next ( error ) ;
59- } else if ( ! ( err instanceof APIError ) ) {
60- const apiError = new APIError ( err . message , err . status , err . isPublic ) ;
61- return next ( apiError ) ;
62- }
63- return next ( err ) ;
54+ if ( err instanceof expressValidation . ValidationError ) {
55+ // validation error contains errors which is an array of error each containing message[]
56+ const unifiedErrorMessage = err . errors . map ( error => error . messages . join ( '. ' ) ) . join ( ' and ' ) ;
57+ const error = new APIError ( unifiedErrorMessage , err . status , true ) ;
58+ return next ( error ) ;
59+ } else if ( ! ( err instanceof APIError ) ) {
60+ const apiError = new APIError ( err . message , err . status , err . isPublic ) ;
61+ return next ( apiError ) ;
62+ }
63+ return next ( err ) ;
6464} ) ;
6565
6666// catch 404 and forward to error handler
6767app . use ( ( req , res , next ) => {
68- const err = new APIError ( 'API not found' , httpStatus . NOT_FOUND ) ;
69- return next ( err ) ;
68+ const err = new APIError ( 'API not found' , httpStatus . NOT_FOUND ) ;
69+ return next ( err ) ;
7070} ) ;
7171
7272// log error in winston transports except when executing test suite
7373if ( config . env !== 'test' ) {
74- app . use ( expressWinston . errorLogger ( {
75- winstonInstance
76- } ) ) ;
74+ app . use ( expressWinston . errorLogger ( {
75+ winstonInstance
76+ } ) ) ;
7777}
7878
7979// error handler, send stacktrace only during development
8080app . use ( ( err , req , res , next ) => // eslint-disable-line no-unused-vars
81- res . status ( err . status ) . json ( {
82- message : err . isPublic ? err . message : httpStatus [ err . status ] ,
83- stack : config . env === 'development' ? err . stack : { }
84- } )
81+ res . status ( err . status ) . json ( {
82+ message : err . isPublic ? err . message : httpStatus [ err . status ] ,
83+ stack : config . env === 'development' ? err . stack : { }
84+ } )
8585) ;
8686
8787export default app ;
0 commit comments