Skip to content

Commit 7a6aa3c

Browse files
committed
Youch package for better error handling in Dev Mode.
1 parent 0db728c commit 7a6aa3c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/server.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const express = require('express')
22
const mongoose = require('mongoose')
3+
const validate = require('express-validation')
4+
const Youch = require('youch')
35

46
const databaseConfig = require('./config/database')
57

@@ -11,6 +13,7 @@ class App {
1113
this.database()
1214
this.middlewares()
1315
this.routes()
16+
this.exception()
1417
}
1518

1619
database () {
@@ -28,6 +31,25 @@ class App {
2831
routes () {
2932
this.express.use(require('./routes'))
3033
}
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+
}
3153
}
3254

3355
module.exports = new App().express

0 commit comments

Comments
 (0)