Skip to content

Commit

Permalink
Vastly improved page error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Feb 26, 2015
1 parent 77b2c86 commit 2867f72
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 39 deletions.
97 changes: 83 additions & 14 deletions roncli.com/admin/controllers/admin_controller.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,111 @@
var handleServerError = require("app/lib/handleServerError");

module.exports = {
/**
* The default admin 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} callback The callback to run upon completion of the controller running.
*/
index: function(params, callback) {
"use strict";

this.app.fetch({
var app = this.app;

app.fetch({
notifications: {model: "User_GetNotifications", params: {}}
}, {readFromCache: false, writeToCache: false}, callback);
}, {readFromCache: false, writeToCache: false}, function(err, result) {
if (!err && result && result.notifications && result.notifications.attributes && result.notifications.attributes.error) {
err = result.notifications.attributes;
}

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

callback(err, result);
});
},

/**
* The blog admin 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} callback The callback to run upon completion of the controller running.
*/
blog: function(params, callback) {
"use strict";

this.app.fetch({
var app = this.app;

app.fetch({
comments: {collection: "Admin_BlogComments", params: {}}
}, {readFromCache: false, writeToCache: false}, callback);
}, {readFromCache: false, writeToCache: false}, function(err, result) {
if (!err && result && result.comments && result.comments.models && result.comments.models[0] && result.comments.models[0].attributes && result.comments.models[0].attributes.error) {
err = result.comments.models[0].attributes;
}

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

callback(err, result);
});
},

/**
* The pages admin 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} callback The callback to run upon completion of the controller running.
*/
pages: function(params, callback) {
"use strict";

this.app.fetch({
var app = this.app;

app.fetch({
pages: {model: "Admin_Pages", params: {}},
comments: {collection: "Admin_PageComments", params: {}}
}, {readFromCache: false, writeToCache: false}, callback);
}, {readFromCache: false, writeToCache: false}, function(err, result) {
if (!err && result && result.pages && result.pages.attributes && result.pages.attributes.error) {
err = result.pages.attributes;
}

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

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

callback(err, result);
});
},

/**
* The page admin 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} callback The callback to run upon completion of the controller running.
*/
page: function(params, callback) {
"use strict";

this.app.fetch({
var app = this.app;

app.fetch({
page: {model: "Admin_Page", params: {url: params[0]}}
}, {readFromCache: false, writeToCache: false}, function(err, result) {
if (!err && result && result.page && result.page.attributes && result.page.attributes.error) {
err = result.page.attributes;
}

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

if (result) {
result.url = params[0];
}
Expand All @@ -60,13 +116,26 @@ module.exports = {
/**
* The music admin 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} callback The callback to run upon completion of the controller running.
*/
music: function(params, callback) {
"use strict";

this.app.fetch({
var app = this.app;

app.fetch({
comments: {collection: "Admin_MusicComments", params: {}}
}, {readFromCache: false, writeToCache: false}, callback);
}, {readFromCache: false, writeToCache: false}, function(err, result) {
if (!err && result && result.comments && result.comments.models && result.comments.models[0] && result.comments.models[0].attributes && result.comments.models[0].attributes.error) {
err = result.comments.models[0].attributes;
}

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

callback(err, result);
});
}
};
44 changes: 42 additions & 2 deletions roncli.com/app/controllers/blog_controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var moment = require("moment");
var handleServerError = require("../lib/handleServerError"),
moment = require("moment");

module.exports = {
/**
* The blog page 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} callback The callback to run upon completion of the controller running.
*/
index: function(params, callback) {
"use strict";
Expand All @@ -15,6 +16,19 @@ module.exports = {
blog: {model: "Blog_GetLatest", params: {}},
categories: {collection: "BlogCategories", params: {}}
}, function(err, result) {
if (!err && result && result.blog && result.blog.attributes && result.blog.attributes.error) {
err = result.blog.attributes;
}

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

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

if (app.req) {
result.meta = {
"og:description": "This is the roncli.com Blog.",
Expand Down Expand Up @@ -49,6 +63,19 @@ module.exports = {
blog: {model: "Blog_GetLatestByCategory", params: {category: params[0]}},
categories: {collection: "BlogCategories", params: {}}
}, function(err, result) {
if (!err && result && result.blog && result.blog.attributes && result.blog.attributes.error) {
err = result.blog.attributes;
}

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

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

if (app.req) {
result.meta = {
"og:description": "This is the " + decodeURIComponent(params[0]) + " category of the roncli.com Blog.",
Expand Down Expand Up @@ -85,6 +112,19 @@ module.exports = {
}, function(err, result) {
var post, content;

if (!err && result && result.blog && result.blog.attributes && result.blog.attributes.error) {
err = result.blog.attributes;
}

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

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

if (app.req) {
post = result.blog.get("post");

Expand Down
24 changes: 3 additions & 21 deletions roncli.com/app/controllers/default_controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var handleServerError = require("../lib/handleServerError");

module.exports = {
/**
* The default view.
Expand All @@ -14,32 +16,12 @@ module.exports = {
}, function(err, result) {
var page, content;

// On the server, a 404 is sent as a valid result. We need to check for this and set err to the result's attributes if this is the case.
// On the client, if there is an error, it is sent under err, and therefore we don't want to mess with it.
if (!err && result && result.page && result.page.attributes && result.page.attributes.error) {
err = result.page.attributes;
}

if (err) {
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);
handleServerError(err, app, result, callback);
return;
}

Expand Down
21 changes: 21 additions & 0 deletions roncli.com/app/controllers/home_controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var handleServerError = require("../lib/handleServerError");

module.exports = {
/**
* The default home view.
Expand All @@ -14,6 +16,25 @@ module.exports = {
songs: {collection: "Song_GetLatest", params: {count: 3}},
classics: {collection: "Song_GetLatestByTag", params: {tag: "Classic", count:3}}
}, function(err, result) {
console.log(err);
console.log(result.classics);
if (!err && result && result.blog && result.blog.attributes && result.blog.attributes.error) {
err = result.blog.attributes;
}

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

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

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

if (app.req) {
result.meta = {
"og:description": "This is the homepage of Ronald M. Clifford.",
Expand Down
47 changes: 46 additions & 1 deletion roncli.com/app/controllers/music_controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var handleServerError = require("../lib/handleServerError");

module.exports = {
/**
* The music view.
Expand All @@ -14,6 +16,23 @@ module.exports = {
songs: {collection: "Song_GetLatest", params: {count: 6}},
tags: {collection: "SongTags", params: {}}
}, function(err, result) {
if (!err && result && result.page && result.page.attributes && result.page.attributes.error) {
err = result.page.attributes;
}

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

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

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

if (app.req) {
result.meta = {
"og:description": "This is the home of roncli, The Nightstalker. Listen to all of The Nightstalker's releases here.",
Expand Down Expand Up @@ -48,6 +67,19 @@ module.exports = {
page: {model: "PageOptional", params: {url: "/music/tag/" + params[0]}},
songs: {collection: "Song_ByTag", params: {tag: params[0]}}
}, function(err, result) {
if (!err && result && result.page && result.page.attributes && result.page.attributes.error) {
err = result.page.attributes;
}

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

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

if (app.req) {
result.meta = {
"og:description": "This is the music of The Nightstalker with the " + decodeURIComponent(params[0]) + " tag.",
Expand Down Expand Up @@ -82,9 +114,22 @@ module.exports = {
app.fetch({
page: {model: "PageOptional", params: {url: "/" + params[0]}},
song: {model: "Song_GetFromUrl", params: {url: "/" + params[0]}}
}, function(err, result) {console.log(app);
}, function(err, result) {
var content;

if (!err && result && result.page && result.page.attributes && result.page.attributes.error) {
err = result.page.attributes;
}

if (!err && result && result.song && result.song.attributes && result.song.attributes.error) {
err = result.song.attributes;
}

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

if (app.req) {
content = result.song.attributes.description.replace("\n", " ").replace("\r", " ").replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
if (content.length === 0) {
Expand Down
Loading

0 comments on commit 2867f72

Please sign in to comment.