File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1
1
const express = require ( 'express' )
2
2
const mongoose = require ( 'mongoose' )
3
+ const validate = require ( 'express-validation' )
4
+ const Youch = require ( 'youch' )
3
5
4
6
const databaseConfig = require ( './config/database' )
5
7
@@ -11,6 +13,7 @@ class App {
11
13
this . database ( )
12
14
this . middlewares ( )
13
15
this . routes ( )
16
+ this . exception ( )
14
17
}
15
18
16
19
database ( ) {
@@ -28,6 +31,25 @@ class App {
28
31
routes ( ) {
29
32
this . express . use ( require ( './routes' ) )
30
33
}
34
+
35
+ exception ( ) {
36
+ this . express . use ( async ( err , req , res , next ) => {
37
+ // Validation Exceptions
38
+ if ( err instanceof validate . ValidationError ) {
39
+ return res . status ( err . status ) . json ( err )
40
+ }
41
+
42
+ if ( this . isDev ) {
43
+ const youch = new Youch ( err , req )
44
+
45
+ return res . json ( await youch . toJSON ( ) )
46
+ }
47
+
48
+ return res
49
+ . status ( err . status || 500 )
50
+ . json ( { error : 'Internal Server Error' } )
51
+ } )
52
+ }
31
53
}
32
54
33
55
module . exports = new App ( ) . express
You can’t perform that action at this time.
0 commit comments