Last active
December 10, 2015 14:28
-
-
Save wookets/4447616 to your computer and use it in GitHub Desktop.
Express 3.x error handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
express = require 'express' | |
error_handler = require './lib/error_handler' | |
module.exports = app = express() | |
app.configure () -> | |
app.use(express.bodyParser()) # standard express http stack | |
app.use(app.router) | |
app.use(error_handler) # attach error handler | |
app.listen(process.env.PORT or 3000) # start server | |
console.log "Express server listening on port #{process.env.PORT or 3000} in #{app.settings.env} mode" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# handle errors that are propogated from route | |
logger = require './logger' | |
emailer = require './emailer' | |
module.exports = (err, req, res, next) -> | |
if not err then return next() # continue if no error | |
logger.error err.stack # log the stack (if you want) | |
# send an email to the developers to let them know an error happened and they need to fix it | |
email = {} | |
email.message = err.stack.replace(new RegExp('\n', 'gi'), '<br/>') | |
emailer.send(email) # no callback, emailer holds default 'to', 'from', 'subject' fields | |
res.jerror(err) # send the user the error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment