Skip to content

Commit 256a94f

Browse files
author
codingChewie
committed
Error Handling Middleware
1 parent 91280b1 commit 256a94f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ app.set('view engine', 'pug')
1111

1212
app.use((req, res, next) => {
1313
const err = new Error('Oh no')
14+
err.status = 500
1415
next(err)
1516
})
1617

@@ -53,4 +54,10 @@ app.post('/goodbye', (req, res) => {
5354
res.redirect('/hello')
5455
})
5556

57+
app.use((err, req, res, next) => {
58+
res.locals.error = err
59+
res.status(err.status)
60+
res.render('error')
61+
})
62+
5663
app.listen(3000, () => console.log('The app is running on port 3000'))

views/error.pug

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends layout.pug
2+
3+
block content
4+
h1= error.message
5+
h2= error.status
6+
pre= error.stack

0 commit comments

Comments
 (0)