Permalink
Browse files

status set on err object to indicate type of error. fixed issues rais…

…ed by jshint.
  • Loading branch information...
kishorenc committed Dec 31, 2011
1 parent 3e234f5 commit a148bc2f25deef57b585d5095acbf37f5b4a0c39
Showing with 20 additions and 17 deletions.
  1. +2 −0 README.md
  2. +17 −16 lib/road.js
  3. +1 −1 package.json
View
@@ -102,6 +102,8 @@ Finally, if you want to register a callback for Road to call when it's done rend
if(err) return next(err);
// do something ...
});
+
+Road sets a `status` property on the `err` object to indicate the type of error. This can be used for rendering view with the correct HTTP status code. For missing controller or controller method, `err.status` is set to 404 (to indicate a missing resource), while any errors passed to Road from the controller is set to 500. If the error object already has a status set, Road does not override that.
##Todo
View
@@ -2,7 +2,7 @@ var fs = require('fs');
var config = {};
-exports.version = '0.2.3';
+exports.version = '0.2.4';
this.configure = function (viewExtension, routes, useLayout, callback) {
config.viewExtension = viewExtension;
@@ -11,20 +11,17 @@ this.configure = function (viewExtension, routes, useLayout, callback) {
config.useLayout = (useLayout === undefined) ? false : useLayout;
config.callback = callback || null;
config.routes.push(['all', '/:controller?/:action?/:id?']);
-}
+};
this.router = function (app) {
for (var index in config.routes) {
- var route = config.routes[index];
+ var route = config.routes[index], method, path;
if (route[0] == 'map') {
- var method = route[1],
- path = route[2],
- controllerName = route[3],
- actionName = route[4];
+ method = route[1]; path = route[2];
+ var controllerName = route[3], actionName = route[4];
app[method](path, mappedResponse(controllerName, actionName));
} else {
- var method = route[0],
- path = route[1];
+ method = route[0]; path = route[1];
app[method](path, directResponse());
}
}
@@ -33,15 +30,15 @@ this.router = function (app) {
function mappedResponse(controllerName, actionName) {
return function(req, res, next) {
callController(controllerName, actionName, req, res, next);
- }
+ };
}
function directResponse() {
return function(req, res, next) {
var controller = req.params.controller || "index";
var action = req.params.action || "index";
- callController(controller, action, req, res, next);
- }
+ callController(controller, action, req, res, next);
+ };
}
function callController(controllerName, functionName, req, res, next) {
@@ -50,14 +47,17 @@ function callController(controllerName, functionName, req, res, next) {
var controllerMethod = req.method.toLowerCase()+'_'+functionName;
controller[controllerMethod](req, res, controllerCallback(controllerName, req, res, next));
} catch(err) {
+ parseError.status = 404;
handleCallback(err, req, res, next);
}
}
function controllerCallback(controllerName, req, res, next) {
return function (err, view, viewData, contentType) {
- if (err) return handleCallback(err, req, res, next);
-
+ if (err) {
+ err.status = err.status || 500;
+ return handleCallback(err, req, res, next);
+ }
if(typeof(view) == 'object' && !(view instanceof Array)) {
for (var cType in view) break;
renderPlainView(view[cType], cType, req, res, next);
@@ -70,15 +70,16 @@ function controllerCallback(controllerName, req, res, next) {
contentType = viewData;
viewData = {};
} else {
- contentType = 'text/html';
+ contentType = 'text/html';
}
}
renderRichView(controllerName, view, viewData, contentType, req, res, next);
} else {
var parseError = new Error("Road - could not parse your view rendering options.");
+ parseError.status = 500;
return handleCallback(parseError, req, res, next);
}
- }
+ };
}
function renderPlainView(content, contentType, req, res, next) {
View
@@ -1,6 +1,6 @@
{
"name": "road",
- "version": "0.2.3",
+ "version": "0.2.4",
"description": "A route helper for express.",
"homepage": "https://github.com/kishorenc/road",
"author": "Kishore Nallan <kishore@kishorelive.com>",

0 comments on commit a148bc2

Please sign in to comment.