Skip to content
This repository
Browse code

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...
commit d4d3c6da876505ccd311046f4be2456d51336634 1 parent 8de521c
Eugene Kashida ekashida authored
23 lib/app/middleware/mojito-handler-error.js
... ... @@ -0,0 +1,23 @@
  1 +/*
  2 + * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
  3 + * Copyrights licensed under the New BSD License.
  4 + * See the accompanying LICENSE file for terms.
  5 + */
  6 +
  7 +/*global require, module*/
  8 +/*jslint sloppy:true, nomen:true*/
  9 +
  10 +/**
  11 + * Export a middleware error handler.
  12 + * @param {Object} The configuration.
  13 + * @return {Object} The handler.
  14 + */
  15 +module.exports = function (config) {
  16 + return function (err, req, res, next) {
  17 + var statusCode = res.statusCode || 500;
  18 + res.send(statusCode, {
  19 + code: statusCode,
  20 + error: err.message
  21 + });
  22 + };
  23 +};
2  lib/app/middleware/mojito-handler-tunnel-parser.js
@@ -74,6 +74,8 @@ module.exports = function (config) {
74 74 path = path.replace(staticPrefix, '')
75 75 .replace(tunnelPrefix, '');
76 76
  77 + req._tunnel = {};
  78 +
77 79 if (path) {
78 80 // Get the basename without the .json extension.
79 81 name = libpath.basename(path, '.json');
6 lib/app/middleware/mojito-handler-tunnel-rpc.js
@@ -36,8 +36,13 @@ module.exports = function (config) {
36 36 command.instance,
37 37 command.context,
38 38 function (err, instance) {
  39 + if (err) {
  40 + next(err);
  41 + }
  42 +
39 43 // Replace with the expanded instance.
40 44 command.instance = instance;
  45 +
41 46 req.command = {
42 47 action: command.action,
43 48 instance: {
@@ -51,6 +56,7 @@ module.exports = function (config) {
51 56 },
52 57 context: command.context
53 58 };
  59 +
54 60 return next();
55 61 }
56 62 );
17 lib/app/middleware/mojito-handler-tunnel-specs.js
@@ -28,7 +28,8 @@ module.exports = function (config) {
28 28 name = specsReq.name;
29 29
30 30 if (!type || !name) {
31   - return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
  31 + res.statusCode = 404;
  32 + return next(new Error('Not found: ' + req.url));
32 33 }
33 34
34 35 instance = {
@@ -45,13 +46,17 @@ module.exports = function (config) {
45 46 req.context,
46 47 function (err, data) {
47 48 if (err) {
48   - return req._tunnel.sendError(
49   - res,
50   - 'Error opening: ' + req.url + '\n' + err,
51   - 500
  49 + res.statusCode = 500;
  50 + return next(
  51 + new Error('Error opening: ' + req.url + '\n' + err)
52 52 );
53 53 }
54   - return req._tunnel.sendData(res, data);
  54 + // TODO: Use the express sugar method res.json([status],
  55 + // [body]) after we rewrite the existing tests.
  56 + res.writeHead(res.statusCode || 200, {
  57 + 'content-type': 'application/json; charset="utf-8"'
  58 + });
  59 + res.end(JSON.stringify(data, null, 4));
55 60 }
56 61 );
57 62 };
17 lib/app/middleware/mojito-handler-tunnel-type.js
@@ -23,7 +23,8 @@ module.exports = function (config) {
23 23 }
24 24
25 25 if (!typeReq.type) {
26   - return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
  26 + res.statusCode = 404;
  27 + return next(new Error('Not found: ' + req.url));
27 28 }
28 29
29 30 instance = {
@@ -36,13 +37,17 @@ module.exports = function (config) {
36 37 req.context,
37 38 function (err, data) {
38 39 if (err) {
39   - return req._tunnel.sendError(
40   - res,
41   - 'Error opening: ' + req.url + '\n' + err,
42   - 500
  40 + res.statusCode = 500;
  41 + return next(
  42 + new Error('Error opening: ' + req.url + '\n' + err)
43 43 );
44 44 }
45   - return req._tunnel.sendData(res, data);
  45 + // TODO: Use the express sugar method res.json([status],
  46 + // [body]) after we rewrite the existing tests.
  47 + res.writeHead(res.statusCode || 200, {
  48 + 'content-type': 'application/json; charset="utf-8"'
  49 + });
  50 + res.end(JSON.stringify(data, null, 4));
46 51 }
47 52 );
48 53 };
13 lib/app/middleware/mojito-handler-tunnel.js
@@ -26,19 +26,6 @@ module.exports = function (config) {
26 26 type
27 27 ];
28 28
29   - // Helper methods.
30   - req._tunnel = {
31   - sendData: function (res, data, code) {
32   - res.writeHead((code || 200), {
33   - 'content-type': 'application/json; charset="utf-8"'
34   - });
35   - res.end(JSON.stringify(data, null, 4));
36   - },
37   - sendError: function (res, msg, code) {
38   - this.sendData(res, {error: msg}, (code || 500));
39   - }
40   - };
41   -
42 29 function run() {
43 30 var m = middleware.shift();
44 31
6 lib/mojito.js
@@ -92,7 +92,8 @@ MojitoServer.MOJITO_MIDDLEWARE = [
92 92 'mojito-contextualizer',
93 93 'mojito-handler-tunnel',
94 94 'mojito-router',
95   - 'mojito-handler-dispatcher'
  95 + 'mojito-handler-dispatcher',
  96 + 'mojito-handler-error'
96 97 ];
97 98
98 99
@@ -358,9 +359,6 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
358 359 // attach middleware pieces
359 360 this._useMiddleware(app, dispatcher, options.dir, midConfig, middleware);
360 361
361   - // TODO: [Issue 82] The last middleware in the stack should be an
362   - // error handler
363   -
364 362 Y.log('Mojito HTTP Server initialized in ' +
365 363 ((new Date().getTime()) - Mojito.MOJITO_INIT) + 'ms.');
366 364 };

0 comments on commit d4d3c6d

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