Skip to content
Browse files

streamline middleware error handling

Uses the convention where a middleware with an arity of 4 is assumed to be the
error handler.
  • Loading branch information...
1 parent 8de521c commit d4d3c6da876505ccd311046f4be2456d51336634 @ekashida ekashida committed Mar 25, 2013
View
23 lib/app/middleware/mojito-handler-error.js
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+
+/*global require, module*/
+/*jslint sloppy:true, nomen:true*/
+
+/**
+ * Export a middleware error handler.
+ * @param {Object} The configuration.
+ * @return {Object} The handler.
+ */
+module.exports = function (config) {
+ return function (err, req, res, next) {
+ var statusCode = res.statusCode || 500;
+ res.send(statusCode, {
+ code: statusCode,
+ error: err.message
+ });
+ };
+};
View
2 lib/app/middleware/mojito-handler-tunnel-parser.js
@@ -74,6 +74,8 @@ module.exports = function (config) {
path = path.replace(staticPrefix, '')
.replace(tunnelPrefix, '');
+ req._tunnel = {};
+
if (path) {
// Get the basename without the .json extension.
name = libpath.basename(path, '.json');
View
6 lib/app/middleware/mojito-handler-tunnel-rpc.js
@@ -36,8 +36,13 @@ module.exports = function (config) {
command.instance,
command.context,
function (err, instance) {
+ if (err) {
+ next(err);
+ }
+
// Replace with the expanded instance.
command.instance = instance;
+
req.command = {
action: command.action,
instance: {
@@ -51,6 +56,7 @@ module.exports = function (config) {
},
context: command.context
};
+
return next();
}
);
View
17 lib/app/middleware/mojito-handler-tunnel-specs.js
@@ -28,7 +28,8 @@ module.exports = function (config) {
name = specsReq.name;
if (!type || !name) {
- return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
+ res.statusCode = 404;
+ return next(new Error('Not found: ' + req.url));
}
instance = {
@@ -45,13 +46,17 @@ module.exports = function (config) {
req.context,
function (err, data) {
if (err) {
- return req._tunnel.sendError(
- res,
- 'Error opening: ' + req.url + '\n' + err,
- 500
+ res.statusCode = 500;
+ return next(
+ new Error('Error opening: ' + req.url + '\n' + err)
);
}
- return req._tunnel.sendData(res, data);
+ // TODO: Use the express sugar method res.json([status],
+ // [body]) after we rewrite the existing tests.
+ res.writeHead(res.statusCode || 200, {
+ 'content-type': 'application/json; charset="utf-8"'
+ });
+ res.end(JSON.stringify(data, null, 4));
}
);
};
View
17 lib/app/middleware/mojito-handler-tunnel-type.js
@@ -23,7 +23,8 @@ module.exports = function (config) {
}
if (!typeReq.type) {
- return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
+ res.statusCode = 404;
+ return next(new Error('Not found: ' + req.url));
}
instance = {
@@ -36,13 +37,17 @@ module.exports = function (config) {
req.context,
function (err, data) {
if (err) {
- return req._tunnel.sendError(
- res,
- 'Error opening: ' + req.url + '\n' + err,
- 500
+ res.statusCode = 500;
+ return next(
+ new Error('Error opening: ' + req.url + '\n' + err)
);
}
- return req._tunnel.sendData(res, data);
+ // TODO: Use the express sugar method res.json([status],
+ // [body]) after we rewrite the existing tests.
+ res.writeHead(res.statusCode || 200, {
+ 'content-type': 'application/json; charset="utf-8"'
+ });
+ res.end(JSON.stringify(data, null, 4));
}
);
};
View
13 lib/app/middleware/mojito-handler-tunnel.js
@@ -26,19 +26,6 @@ module.exports = function (config) {
type
];
- // Helper methods.
- req._tunnel = {
- sendData: function (res, data, code) {
- res.writeHead((code || 200), {
- 'content-type': 'application/json; charset="utf-8"'
- });
- res.end(JSON.stringify(data, null, 4));
- },
- sendError: function (res, msg, code) {
- this.sendData(res, {error: msg}, (code || 500));
- }
- };
-
function run() {
var m = middleware.shift();
View
6 lib/mojito.js
@@ -92,7 +92,8 @@ MojitoServer.MOJITO_MIDDLEWARE = [
'mojito-contextualizer',
'mojito-handler-tunnel',
'mojito-router',
- 'mojito-handler-dispatcher'
+ 'mojito-handler-dispatcher',
+ 'mojito-handler-error'
];
@@ -358,9 +359,6 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
// attach middleware pieces
this._useMiddleware(app, dispatcher, options.dir, midConfig, middleware);
- // TODO: [Issue 82] The last middleware in the stack should be an
- // error handler
-
Y.log('Mojito HTTP Server initialized in ' +
((new Date().getTime()) - Mojito.MOJITO_INIT) + 'ms.');
};

0 comments on commit d4d3c6d

Please sign in to comment.
Something went wrong with that request. Please try again.