Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improved error handling when running controllers

  • Loading branch information...
commit 245085051afea5911212b9864268c807f41f6b0e 1 parent 5f535dd
@antz29 authored
Showing with 39 additions and 31 deletions.
  1. +39 −31 src/lib/base.js
View
70 src/lib/base.js
@@ -111,43 +111,51 @@ module.exports = (function() {
fs.stat(target, function(err) {
if (err) return callback(null);
- controller = require(target);
- controller.getBase = function() { return that; }
-
- callPreAction(function() {
- var method = req.method.toLowerCase();
- var method_action = req.action + '_' + method;
-
- var controller_action;
-
- if (controller[method_action]) {
- controller_action = controller[req.action + '_' + method];
- }
- else if (controller[req.action]) {
- controller_action = controller[req.action];
- }
+ try {
+ controller = require(target);
- controller_action(req,res,function(td) {
- tdata = _.extend(tdata, td);
- callback(null);
- process.nextTick(callPostAction);
- });
- });
+ controller.getBase = function() { return that; }
- function callPreAction(callback) {
- if (controller.pre_action) {
- return controller.pre_action(req,res,callback);
+ callPreAction(function() {
+ var method = req.method.toLowerCase();
+ var method_action = req.action + '_' + method;
+
+ var controller_action;
+
+ if (controller[method_action]) {
+ controller_action = controller[req.action + '_' + method];
+ }
+ else if (controller[req.action]) {
+ controller_action = controller[req.action];
+ }
+
+ controller_action(req,res,function(td) {
+ tdata = _.extend(tdata, td);
+ callback(null);
+ process.nextTick(callPostAction);
+ });
+ });
+
+ function callPreAction(callback) {
+ if (controller.pre_action) {
+ return controller.pre_action(req,res,callback);
+ }
+ callback();
}
- callback();
- }
- function callPostAction() {
- if (controller.post_action) {
- process.nextTick(function() {
- controller.post_action(req);
- });
+ function callPostAction() {
+ if (controller.post_action) {
+ process.nextTick(function() {
+ controller.post_action(req);
+ });
+ }
}
}
+ catch (e) {
+ console.log('Error loading controller:\n', e);
+ res.statusCode = 500;
+ return res.end("<h1>500 Internal Server Error</h1>");
+ }
});
},
'view' : function(callback) {
Please sign in to comment.
Something went wrong with that request. Please try again.