Permalink
Browse files

Make the error handler configurable

Change-Id: Iff393c59f24cda138f2c6a4a3a2470d2c3e3b567
  • Loading branch information...
1 parent bf52ad4 commit de49b800af588f80dad61400bd46f7a817438d85 @creationix committed Dec 26, 2010
Showing with 15 additions and 15 deletions.
  1. +1 −1 README.markdown
  2. +14 −14 stack.js
View
@@ -47,7 +47,7 @@ Besides providing this nice linear syntax for defining http handler layers Stack
- Wraps each layer in a `try..catch` to catch any exceptions that happen in the main execution stack layer's handler function.
- Provides a fallthrough error handler function that returns 404 for routes that fall through all the layers and 500 responses for exceptions.
- (In the future, I want to be able to override this function.)
+ (You can override this at Stack.errorHandler, if for example, you don't like showing stack traces for all errors or want to pretty it up a bit)
- Forwards and exceptions passed to any next layer directly to the error handler. This means layers don't have to worry about errors from previous layers.
## What Stack Does NOT Do
View
@@ -1,27 +1,27 @@
-
-module.exports = function Stack(/*layers*/) {
- function error(req, res, err) {
- if (err) {
- res.writeHead(500, {"Content-Type": "text/plain"});
- res.end(err.stack + "\n");
- return;
- }
- res.writeHead(404, {"Content-Type": "text/plain"});
- res.end("Not Found\n");
- }
- var handle = error;
+function Stack(/*layers*/) {
+ var handle = Stack.error;
Array.prototype.slice.call(arguments).reverse().forEach(function (layer) {
var child = handle;
handle = function (req, res) {
try {
layer(req, res, function (err) {
- if (err) { return error(req, res, err); }
+ if (err) { return Stack.error(req, res, err); }
child(req, res);
});
} catch (err) {
- error(req, res, err);
+ Stack.error(req, res, err);
}
};
});
return handle;
+}
+Stack.errorHandler = function error(req, res, err) {
+ if (err) {
+ res.writeHead(500, {"Content-Type": "text/plain"});
+ res.end(err.stack + "\n");
+ return;
+ }
+ res.writeHead(404, {"Content-Type": "text/plain"});
+ res.end("Not Found\n");
};
+module.exports = Stack;

0 comments on commit de49b80

Please sign in to comment.