Skip to content

Commit 23af839

Browse files
committed
chore(js): move an exception handler in order to catch exceptions from custom routes
Part of #27 Relate to #48
1 parent 9eaeb80 commit 23af839

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

examples/js/express/mysql/app.js

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ const pool = mysql.createPool({
3636
routes.register(app, pool)
3737
custom_routes.register(app, pool)
3838

39+
app.use((error, req, res, next) => {
40+
console.error(error)
41+
res.status(500).json({ "error": "Internal Server Error" })
42+
})
43+
3944
const port = process.env.PORT || 3000
4045
app.listen(port, () => {
4146
console.log(`Listen on ${port}`)

examples/js/express/mysql/custom_routes.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ exports.register = (app, pool) => {
44
res.json({ "custom": true })
55
})
66

7+
app.get('/custom/exception', (req, res, next) => {
8+
throw new Error('expected err')
9+
})
10+
711
}

examples/js/express/mysql/routes.js

-4
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ const register = (app, pool) => {
189189
)
190190
})
191191

192-
app.use((error, req, res, next) => {
193-
console.error(error)
194-
res.status(500).json({ "error": "Internal Server Error" })
195-
})
196192
}
197193

198194
exports.register = register

src/templates/app.js.ejs

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ routes.register(app, pool)
5252
<%- routerName %>.register(app, pool)
5353
<% }) -%>
5454

55+
app.use((error, req, res, next) => {
56+
console.error(error)
57+
res.status(500).json({ "error": "Internal Server Error" })
58+
})
59+
5560
const port = process.env.PORT || 3000
5661
app.listen(port, () => {
5762
console.log(`Listen on ${port}`)

src/templates/routes.js.ejs

-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ endpoints.forEach(function(endpoint) {
8686
})
8787
})
8888
%>
89-
app.use((error, req, res, next) => {
90-
console.error(error)
91-
res.status(500).json({ "error": "Internal Server Error" })
92-
})
9389
}
9490

9591
exports.register = register

0 commit comments

Comments
 (0)