Skip to content

Commit

Permalink
Implement error handling middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Jun 21, 2023
1 parent 61a97c9 commit fb80222
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions services/users/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,19 @@ app.use('/seed', seed)
/** Returns 401 to the client in the case of session related errors */
app.use(errorHandler())

/* Handle 500 errors */
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
// Log the error stack trace
console.error(`Error stack trace in middleware: ${err.stack}`)
console.error(`Error message in middleware: ${err.message}`)

// Check if headers have already been sent to the client
if (res.headersSent) {
return next(err)
}
console.log('res.statusMessage :>> ', res.statusMessage)
res.status(500).send('Server error.')
})

app.listen(port)
console.log(`Users server listening on port ${port}`)

0 comments on commit fb80222

Please sign in to comment.