Skip to content
Permalink
Browse files

Finally replaced development errorhandler with a custom routine, and …

…also added patterns for handling errors on both the server and client sides.
  • Loading branch information...
roncli committed May 31, 2016
1 parent 39cd82c commit 30f20d4352b1b4682e24e0564f9dd17385853559
@@ -11,6 +11,7 @@ Features
* Provides working examples of how to work with models and collections in the controller and templates.
* Includes JavaScript, CSS, and HTML compression in an automated grunt build process.
* Includes the latest and most compatible versions of the core modules used with rendr.
* Patterns on how to handle errors using an error handler module for both Rendr and Express, and what to do about API errors on the front end.

Installing
==========
@@ -25,7 +26,7 @@ Status

This is the list of direct package dependencies that have yet to be upgraded to the latest version or are not final, along with the reasons why they are not upgraded. If an outdated package is not listed here, I simply haven't gotten around to looking at it yet.

* errorhandler - This is not the final error handler, as it's meant for development only. I will look into getting a more production-ready error handler.
* Everything is up to date! Yay!

Got Better?
===========
@@ -2,16 +2,12 @@ module.exports = {
/**
* The default view.
* @param {object} params The parameters to use in the controller.
* @param {function((null | object), object=)} callback The callback to run upon completion of the controller running.
* @param {function((null | object)=, object=, object=)} callback The callback to run upon completion of the controller running.
*/
index: function(params, callback) {
"use strict";

// This is a 404.
if (this.app && this.app.req && this.app.req.res) {
this.app.req.res.status(404);
}

callback(null);
callback(null, "error/404");
}
};
@@ -1,8 +1,10 @@
var handleServerError = require("../lib/handleServerError");

module.exports = {
/**
* The default home view.
* @param {object} params The parameters to use in the controller.
* @param {function((null | object), object=)} callback The callback to run upon completion of the controller running.
* @param {function((null | object)=, object=, object=)} callback The callback to run upon completion of the controller running.
*/
index: function(params, callback) {
"use strict";
@@ -14,8 +16,22 @@ module.exports = {
};

app.fetch(data, function(err, result) {
// You should always check your models and collections for errors, and redirect to an error page as necessary.
if (!err && result && result.time && result.time.attributes && result.time.attributes.error) {
err = result.time.attributes;
}

if (!err && result && result.users && result.users.models && result.users.models[0] && result.users.models[0].attributes && result.users.models[0].attributes.error) {
err = result.users.models[0].attributes;
}

if (err) {
handleServerError(err, app, result, callback);
return;
}

// TODO: This is how you can dynamically set meta tags from the controller. Example tags are for use with Google+, Facebook, and Twitter.
if (app.req) {
// TODO: This is how you can dynamically set meta tags from the controller. Example tags are for use with Google+, Facebook, and Twitter.
result.meta = {
"article:author": "http://www.facebook.com/[your Facebook url]",
"article:published_time": "2015-01-14T15:57:47-06:00",
@@ -41,6 +57,11 @@ module.exports = {
});
},

/**
* The page view.
* @param {object} params The parameters to use in the controller.
* @param {function((null | object)=, object=, object=)} callback The callback to run upon completion of the controller running.
*/
page: function(params, callback) {
"use strict";

@@ -0,0 +1,30 @@
/**
* A function to handle a server error in the controllers.
* @param {object} err The error object.
* @param {object} app The application object.
* @param {object} result The result object.
* @param {function((null | object)=, object=, object=)} callback The callback function.
*/
module.exports = function(err, app, result, callback) {
"use strict";

if (err.status) {
// This is a known error.
if (app && app.req && app.req.res) {
app.req.res.status(err.status);
}

if (err.status === 404) {
callback(null, "error/404", result);
} else {
callback(null, "error/other", result);
}
return;
}

// This is an unknown error.
if (app && app.req && app.req.res) {
app.req.res.status(500);
}
callback(null, "error/other", result);
};
@@ -1,2 +1,3 @@
<h1>404</h1>
<div>We're sorry, but this page doesn't exist.</div>
<div><a href="/">Return Home</a></div>
@@ -0,0 +1,3 @@
<h1>Error</h1>
<div>We're sorry, but a server error ocurred.</div>
<div><a href="/">Return Home</a></div>
@@ -5,4 +5,4 @@
<li>{{attributes.name}}</li>
{{/each}}
</ul>
<div><a href="/page">Visit the page</a></div>
<div><a href="/page">Visit the page</a></div>
@@ -1,2 +1,2 @@
<div>This is a new page.</div>
<div><a href="/">Return Home</a></div>
<div><a href="/">Return Home</a></div>

This file was deleted.

@@ -0,0 +1,8 @@
var BaseView = require("rendr/shared/base/view");

// Sets up the 404 view.
module.exports = BaseView.extend({
className: "error_404_view"
});

module.exports.id = "error/404";
@@ -0,0 +1,8 @@
var BaseView = require("rendr/shared/base/view");

// Sets up the other error view.
module.exports = BaseView.extend({
className: "error_other_view"
});

module.exports.id = "error/other";
@@ -9,25 +9,13 @@ var express = require("express"),
bodyParser = require("body-parser"),
app = express(),
ApiDataAdapter = require("./server/ApiDataAdapter"),
errorHandler = require("./server/errorHandler"),
server = rendr.createServer({
dataAdapter: new ApiDataAdapter(),
errorHandler: require("errorhandler")
errorHandler: errorHandler
});

// Initialize middleware stack.
app.use(compression());
app.use(morgan("[:date] :remote-addr :method :url HTTP/:http-version :status :res[content-length] \":user-agent\" :response-time \":referrer\""));
app.use(cookieParser("tmp"));
app.use(session({
secret: "tmp",
resave: false,
saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// Setup domains.
app.use(function(req, res, next) {
"use strict";

@@ -42,12 +30,24 @@ app.use(function(req, res, next) {
d.on("error", function(err) {
console.log("Domain error");
console.log(err);
next(err);
});

d.run(next);
});

app.use(errorHandler);
app.use(compression());
app.use(morgan("[:date] :remote-addr :method :url HTTP/:http-version :status :res[content-length] \":user-agent\" :response-time \":referrer\""));
app.use(cookieParser("tmp"));
app.use(session({
secret: "tmp",
resave: false,
saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// Add the rendr server.
app.use(server.handle);

@@ -60,6 +60,7 @@ ApiDataAdapter.prototype.request = function(req, api, options, callback) {
console.log("Unknown server error.");
console.log(err);
console.log(err.stack);
console.log(req);
req.res.status(500);
callback(null, req.res, {error: "Unknown server error."});
} catch (err2) {
@@ -0,0 +1,11 @@
module.exports = function(err, req, res, next) {
"use strict";

var status = err.status || 500,
message = err.message || "Internal Server Error";

res.status(status);
res.send("<h1>Error " + status + " occurred</h1><h2>" + message + "</h2>");

// Suggested TODO: Log your errors! err should include the stack trace, and you can also save the request object as well.
};

0 comments on commit 30f20d4

Please sign in to comment.
You can’t perform that action at this time.