Skip to content

Log unhandled errors that occur upon startup or handling HTTP requests #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ glob.sync(scriptsToLoad).forEach((file) => {
require(file)(app)
})

app.use(function logUnhandledErrors (err, req, res, next) {
logger.error(err, 'Unhandled error while responding to incoming HTTP request')
res.status(500).end()
})

module.exports = app
10 changes: 10 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ if (process.env.SSE_RELAY) {
}
}
}

function logUnhandledException (err) {
logger.fatal(err, 'Unchaught exception, terminating bot process immediately')

// leave time for error to be written to disk before exiting process
setTimeout(() => process.exit(1), 10)
}

process.on('uncaughtException', logUnhandledException)
process.on('unhandledRejection', logUnhandledException)