Permalink
Browse files

Added custom error handling and cleaned up some of the Jade templates…

…. Also specified package versions in require
  • Loading branch information...
1 parent f2261c5 commit 929f5642ca1b2fa664df517457e056c92490d892 Alex Young committed Dec 27, 2010
View
62 app.js
@@ -1,7 +1,8 @@
-var express = require('express'),
+var express = require('express@1.0.0'),
app = module.exports = express.createServer(),
- mongoose = require('mongoose').Mongoose,
- mongoStore = require('connect-mongodb'),
+ mongoose = require('mongoose@0.0.4').Mongoose,
+ mongoStore = require('connect-mongodb@0.0.4'),
+ sys = require('sys'),
db,
Document,
User,
@@ -19,6 +20,7 @@ function mongoStoreConnectionArgs() {
app.configure('development', function() {
app.set('db-uri', 'mongodb://localhost/nodepad-development');
+ app.use(express.errorHandler({ dumpExceptions: true }));
});
app.configure('test', function() {
@@ -42,7 +44,6 @@ app.configure(function() {
app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' }))
app.use(express.methodOverride());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
- app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
});
@@ -68,6 +69,44 @@ app.get('/', loadUser, function(req, res) {
res.redirect('/documents')
});
+// Error handling
+function NotFound(msg) {
+ this.name = 'NotFound';
+ Error.call(this, msg);
+ Error.captureStackTrace(this, arguments.callee);
+}
+
+sys.inherits(NotFound, Error);
+
+app.get('/404', function(req, res) {
+ throw new NotFound;
+});
+
+app.get('/500', function(req, res) {
+ throw new Error('An expected error');
+});
+
+app.get('/bad', function(req, res) {
+ unknownMethod();
+});
+
+app.error(function(err, req, res, next) {
+ if (err instanceof NotFound) {
+ res.render('404.jade', { status: 404 });
+ } else {
+ next(err);
+ }
+});
+
+app.error(function(err, req, res) {
+ res.render('500.jade', {
+ status: 500,
+ locals: {
+ error: err
+ }
+ });
+});
+
// Document list
app.get('/documents.:format?', loadUser, function(req, res) {
Document.find().all(function(documents) {
@@ -86,8 +125,9 @@ app.get('/documents.:format?', loadUser, function(req, res) {
});
});
-app.get('/documents/:id.:format?/edit', loadUser, function(req, res) {
+app.get('/documents/:id.:format?/edit', loadUser, function(req, res, next) {
Document.findById(req.params.id, function(d) {
+ if (!d) return next(new NotFound('Document not found'));
res.render('documents/edit.jade', {
locals: { d: d, currentUser: req.currentUser }
});
@@ -116,8 +156,10 @@ app.post('/documents.:format?', loadUser, function(req, res) {
});
// Read document
-app.get('/documents/:id.:format?', loadUser, function(req, res) {
+app.get('/documents/:id.:format?', loadUser, function(req, res, next) {
Document.findById(req.params.id, function(d) {
+ if (!d) return next(new NotFound('Document not found'));
+
switch (req.params.format) {
case 'json':
res.send(d.__doc);
@@ -132,8 +174,10 @@ app.get('/documents/:id.:format?', loadUser, function(req, res) {
});
// Update document
-app.put('/documents/:id.:format?', loadUser, function(req, res) {
+app.put('/documents/:id.:format?', loadUser, function(req, res, next) {
Document.findById(req.body.d.id, function(d) {
+ if (!d) return next(new NotFound('Document not found'));
+
d.title = req.body.d.title;
d.data = req.body.d.data;
d.save(function() {
@@ -150,8 +194,10 @@ app.put('/documents/:id.:format?', loadUser, function(req, res) {
});
// Delete document
-app.del('/documents/:id.:format?', loadUser, function(req, res) {
+app.del('/documents/:id.:format?', loadUser, function(req, res, next) {
Document.findById(req.params.id, function(d) {
+ if (!d) return next(new NotFound('Document not found'));
+
d.remove(function() {
switch (req.params.format) {
case 'json':
View
@@ -1,4 +1,4 @@
-var mongoose = require('mongoose').Mongoose,
+var mongoose = require('mongoose@0.0.4').Mongoose,
crypto = require('crypto');
mongoose.model('Document', {
View
@@ -11,7 +11,7 @@
},
"dependencies": {
"express": "1.0.0",
- "mongoose": "0.0.",
+ "mongoose": "0.0.4",
"connect-mongodb": "0.0.4",
"jade": "0.6.0"
}
@@ -162,3 +162,12 @@ ul.toolbar a {
font-size: 13px;
width: 44px;
}
+.page {
+ padding: 10px;
+}
+.page h2 {
+ margin-bottom: 10px;
+}
+.page p {
+ margin: 10px 0;
+}
@@ -179,3 +179,15 @@ ul.toolbar a {
font-size: 13px;
width: 44px
}
+
+.page {
+ padding: 10px;
+}
+
+.page h2 {
+ margin-bottom: 10px;
+}
+
+.page p {
+ margin: 10px 0;
+}
View
@@ -0,0 +1,3 @@
+.page
+ h2 Page Not Found
+ p That page could not be found.
View
@@ -0,0 +1,5 @@
+.page
+ h2 Error
+ p Something went wrong with the application.
+ h3 Error Details
+ pre #{error}
@@ -1,6 +1,6 @@
h2 Edit Document
p
- a(href='/documents', class='button')
+ a.button(href='/documents')
← All Documents
form(method='post', action='/documents/' + d.id)
input(name='d[id]', value=d.id, type='hidden')
View
@@ -1,25 +1,25 @@
-div(class='outline-view', id='left')
- div(id='DocumentTitles')
- ul(id='document-list')
+#left.outline-view
+ #DocumentTitles
+ ul#document-list
- for (var d in documents)
li
a(id='document-title-' + documents[d].id, href='/documents/' + documents[d].id)
=documents[d].title
- ul(class='toolbar')
+ ul.toolbar
li
a(href='/documents/new')
+
li
- a(href='#', id='delete-document')
+ a#delete-document(href='#')
-
-div(class='content-divider')
+.content-divider
-div(class='content')
- div(id='editor-container')
- textarea(name='d[data]', id='editor')
+.content
+ #editor-container
+ textarea#editor(name='d[data]')
-ul(id='controls', class='toolbar')
+ul#controls.toolbar
li
- a(href='#', id='save-button') Save
+ a#save-button(href='#') Save
View
@@ -1,6 +1,7 @@
-h2 New Document
-p
- a(href='/documents', class='button') ← All Documents
-form(method='post', action='/documents')
- !=partial('documents/fields', { locals: { d: d } })
+.page
+ h2 New Document
+ p
+ a.button(href='/documents') ← All Documents
+ form(method='post', action='/documents')
+ !=partial('documents/fields', { locals: { d: d } })
@@ -1,6 +1,6 @@
h2=d.title
- a(class='button', href='/documents/' + d.id + '/edit') Edit
+ a.button(href='/documents/' + d.id + '/edit') Edit
p
- a(href='/documents', class='button')
+ a.button(href='/documents')
← All Documents
p=d.data
View
@@ -8,14 +8,14 @@ html
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js')
body
- div(id='container')
- div(id='header')
+ #container
+ #header
ul
li
h1
a(href='/') Nodepad
- if (typeof currentUser !== 'undefined')
- li(class='right')
- a(href='/sessions', class='destroy') Log Out
+ li.right
+ a.destroy(href='/sessions') Log Out
!= body
script(type='text/javascript', src='/javascripts/application.js')
View
@@ -1,8 +1,9 @@
-h2 Log In
-p
- a(href='/users/new', class='button') Register
-form(method='post', action='/sessions')
- !=partial('users/fields', { locals: { user: user } })
- div
- input(type='submit', value='Log In')
+.page
+ h2 Log In
+ p
+ a.button(href='/users/new') Register
+ form(method='post', action='/sessions')
+ !=partial('users/fields', { locals: { user: user } })
+ div
+ input(type='submit', value='Log In')
View
@@ -1,9 +1,10 @@
-h2 Register
+.page
+ h2 Register
-form(method='post', action='/users')
- !=partial('users/fields', { locals: { user: user } })
- div
- input(type='submit', value='Register')
- span \ or
- a(href='/sessions/new') Log In
+ form(method='post', action='/users')
+ !=partial('users/fields', { locals: { user: user } })
+ div
+ input(type='submit', value='Register')
+ span \ or
+ a(href='/sessions/new') Log In

0 comments on commit 929f564

Please sign in to comment.