forked from creationix/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I5aff679a93b7f00b8963cc97bc38dfe1ad36e517
- Loading branch information
0 parents
commit 8af8c4d
Showing
3 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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; | ||
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); } | ||
child(req, res); | ||
}); | ||
} catch (err) { | ||
error(req, res, err); | ||
} | ||
}; | ||
}); | ||
return handle; | ||
}; |