Created
June 28, 2013 18:04
-
-
Save rgarcia/5886729 to your computer and use it in GitHub Desktop.
useful domain error handling example from nodeconf
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
var util = require("util"); | |
var domain = require("domain"); | |
var express = require("express"); | |
var defaultErrHandler = express.errorHandler(); | |
module.exports = function totallyUseless(error, req, res, next) { | |
console.error("in error handler", error.stack) | |
if (domain.active) { | |
domain.active.emit("error", error); | |
} else { | |
defaultErrHandler(error, req, res, next); | |
} | |
}; |
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
"use strict"; | |
var http = require("http"); | |
var domain = require("domain"); | |
var path = require("path"); | |
var express = require("express"); | |
var domainErrors = require("./domain-errors.js"); | |
function databaseCall() { | |
throw new Error("oh no the database exploded"); | |
} | |
var app = express(); | |
app.set("port", process.env.PORT || 1337); | |
app.set("views", __dirname + "/views"); | |
app.set("view engine", "jade"); | |
app.enable('verbose errors'); | |
app.use(express.favicon()); | |
app.use(express.logger("dev")); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(path.join(__dirname, "public"))); | |
app.use(domainErrors); | |
app.get("/", function (req, res) { | |
res.render("index", {title : "Express"}); | |
}); | |
app.get("/throw", function (req, res) { | |
void res; // shut up, jshint | |
throw new Error("Imagine I'm deeper in the code"); | |
}); | |
app.get("/database", function (req, res, next) { | |
setTimeout(function () { | |
databaseCall(); | |
next(); | |
}, 500); | |
}); | |
// route-specific domains for errors we can intercept and not crash on | |
app.get("/handle", function (req, res) { | |
var subDomain = domain.create(); | |
subDomain.on("error", function (error) { | |
res.send(500, "<html><head><title>whoops</title></head><body>" + | |
"<p>Got error: '" + error.message + "'. Ignoring.</p>" + | |
"</body></html>"); | |
}); | |
subDomain.run(databaseCall); | |
}); | |
// maindomain is for "unknown" errors, just bail | |
var mainDomain = domain.create(); | |
mainDomain.on("error", function (error) { | |
console.error("caught %s in main domain, shutting down", error.message); | |
process.exit(1); | |
}); | |
mainDomain.run(function () { | |
http.createServer(app).listen(app.get("port"), function () { | |
console.log("Express server listening on port " + app.get("port")); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment