@@ -17,7 +17,7 @@ import APIError from '../server/helpers/APIError';
17
17
const app = express ( ) ;
18
18
19
19
if ( config . env === 'development' ) {
20
- app . use ( logger ( 'dev' ) ) ;
20
+ app . use ( logger ( 'dev' ) ) ;
21
21
}
22
22
23
23
// parse body params and attache them to req.body
@@ -36,52 +36,52 @@ app.use(cors());
36
36
37
37
// enable detailed API logging in dev env
38
38
if ( 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
+ } ) ) ;
47
47
}
48
48
49
49
// mount all routes on /api path
50
50
app . use ( '/api' , routes ) ;
51
51
52
52
// if error is not an instanceOf APIError, convert it.
53
53
app . 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 ) ;
64
64
} ) ;
65
65
66
66
// catch 404 and forward to error handler
67
67
app . 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 ) ;
70
70
} ) ;
71
71
72
72
// log error in winston transports except when executing test suite
73
73
if ( config . env !== 'test' ) {
74
- app . use ( expressWinston . errorLogger ( {
75
- winstonInstance
76
- } ) ) ;
74
+ app . use ( expressWinston . errorLogger ( {
75
+ winstonInstance
76
+ } ) ) ;
77
77
}
78
78
79
79
// error handler, send stacktrace only during development
80
80
app . 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
+ } )
85
85
) ;
86
86
87
87
export default app ;
0 commit comments