Skip to content
This repository

Flag for bypassing default error pages #251

Merged
merged 4 commits into from almost 2 years ago

2 participants

Nick Crohn Benjamin Arthur Lupton
Nick Crohn
ncrohn commented June 28, 2012

Added in a docpad config variable to allow the errors plugin to function.

This allows for the server to respond in a custom fashion to 404 pages and the like.

Benjamin Arthur Lupton
Owner

Sweet, good work!

The plugin you've created for this could probably go straight into the core too. What are your thoughts?

Nick Crohn
ncrohn commented July 02, 2012

I think that probably makes a lot of sense. I will get the plugin moved over to core today and added to this pull request.

Nick Crohn
ncrohn commented July 02, 2012

Moved the error plugin to the core page handling, also changed the name of the custom errors flag to useCustomErrors. I had it removed but thought better of it for people wanting to use an htaccess file.

Let me know what you think.

Benjamin Arthur Lupton
Owner

Instead of hitting the file system for this, we could check the database which would be faster, something like this:

database = docpad.getDatabase()
document = docpad.findOne(relativePath: '404.html')
res.send(document.get('contentRendered') or document.get('content'), 404)

It also seems that we can only detect/send 404 and 500 errors, but yet we render 4xx and 5xx pages as well. This seems a bit overreaching vs just rendering what we can detect. Your thoughts?

Good call on the database feature. That would definitely be faster and more inline with how docpad works.

To make the 4xx and 5xx features more useful, I agree there would probably need to be a more full-fledged error detection to serve proper error pages. For what docpad is and is intended to solve that might not be something it needs. For now I think just having a 404.html and a 500.html page should suffice. Do you think there should be effort put in to expanding what error codes can be served? If not I can make the changes pretty quickly to only server the 404 and 500.

Nick Crohn
ncrohn commented July 12, 2012

Do you have any other feedback on this addition? The recent commit drops the excessive error handling.

Benjamin Arthur Lupton
Owner

Sweet. Didn't spot that last commit. Will merge in soon :)

Nick Crohn
ncrohn commented July 18, 2012

Wanted to check on the status, we are currently running off of a fork for this and would be nice to have it in your deployed version of docpad. Let me know if there is anything else I can do. Thanks again!

Benjamin Arthur Lupton
Owner

Doing it now :)

Benjamin Arthur Lupton balupton referenced this pull request from a commit July 19, 2012
Benjamin Arthur Lupton Pulled #251, Fixed #269, #268
We now only clear the meta attributes from the document attributes on a
new parse. Instead of clearing everything. Other parsing optimisations
were also made.

File IDs are now also the relativePath instead of the relativeBase to
avoid collisions.
b52226c
Benjamin Arthur Lupton balupton merged commit 09de510 into from July 18, 2012
Benjamin Arthur Lupton balupton closed this July 18, 2012
Benjamin Arthur Lupton
Owner

Published in v6.4.0

Benjamin Arthur Lupton
Owner

Btw, working fantastic on the kitchensink site: http://docpad-kitchensink.herokuapp.com/asdasdsad

:D

Nick Crohn
ncrohn commented July 19, 2012

Awesome! Thanks for getting this in!

Benjamin Arthur Lupton balupton referenced this pull request from a commit July 19, 2012
Benjamin Arthur Lupton Pulled #251, Fixed #269, #268
We now only clear the meta attributes from the document attributes on a
new parse. Instead of clearing everything. Other parsing optimisations
were also made.

File IDs are now also the relativePath instead of the relativeBase to
avoid collisions.
cadbcf3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 62 additions and 3 deletions. Show diff stats Hide diff stats

  1. 65  src/lib/docpad.coffee
65  src/lib/docpad.coffee
@@ -3,6 +3,7 @@
3 3
 
4 4
 # Necessary
5 5
 pathUtil = require('path')
  6
+fsUtil = require('fs')
6 7
 _ = require('underscore')
7 8
 caterpillar = require('caterpillar')
8 9
 CSON = require('cson')
@@ -104,6 +105,32 @@ class DocPad extends EventEmitterEnhanced
104 105
 	getEvents: ->
105 106
 		@events
106 107
 
  108
+	errors:
  109
+		'400': 'Bad Request'
  110
+		'401': 'Unauthorized'
  111
+		'402': 'Payment Required'
  112
+		'403': 'Forbidden'
  113
+		'404': 'Not Found'
  114
+		'405': 'Method Not Allowed'
  115
+		'406': 'Not Acceptable'
  116
+		'407': 'Proxy Authentication Required'
  117
+		'408': 'Request Timeout'
  118
+		'409': 'Conflict'
  119
+		'410': 'Gone'
  120
+		'411': 'Length Required'
  121
+		'412': 'Precondition Failed'
  122
+		'413': 'Request Entity Too Large'
  123
+		'414': 'Request-URI Too Long'
  124
+		'415': 'Unsupported Media Type'
  125
+		'416': 'Requested Range Not Satisfiable'
  126
+		'417': 'Expectation Failed'
  127
+		'500': 'Internal Server Error'
  128
+		'501': 'Not Implemented'
  129
+		'502': 'Bad Gateway'
  130
+		'503': 'Service Unavailable'
  131
+		'504': 'Gateway Timeout'
  132
+		'505': 'HTTP Version Not Supported'
  133
+
107 134
 	# ---------------------------------
108 135
 	# Collections
109 136
 
@@ -329,6 +356,10 @@ class DocPad extends EventEmitterEnhanced
329 356
 		# Whether or not we should extend the server with extra middleware and routing
330 357
 		extendServer: true
331 358
 
  359
+		# Enable Custom Error Pages
  360
+		# A flag to provide an entry to handle custom error pages
  361
+		useCustomErrors: false
  362
+
332 363
 		# Port
333 364
 		# The port that the server should use
334 365
 		port: 9778
@@ -2412,7 +2443,7 @@ class DocPad extends EventEmitterEnhanced
2412 2443
 									contentRendered = document.get('contentRendered')
2413 2444
 									if err
2414 2445
 										docpad.error(err)
2415  
-										return res.send(err.message, 500)
  2446
+										return next(err)
2416 2447
 									else
2417 2448
 										return res.send(contentRendered)
2418 2449
 							else
@@ -2428,8 +2459,36 @@ class DocPad extends EventEmitterEnhanced
2428 2459
 							server.use(express.static config.outPath)
2429 2460
 
2430 2461
 						# 404 Middleware
2431  
-						server.use (req,res,next) ->
2432  
-							return res.send(404)
  2462
+						server.use (req, res, next) ->
  2463
+							database = docpad.getDatabase()
  2464
+							return next() unless database
  2465
+							notFound = 404
  2466
+							if config.useCustomErrors
  2467
+								file = database.findOne(relativePath: '404.html')
  2468
+								if file
  2469
+									data = file.get('contentRendered') or document.get('content')
  2470
+								else
  2471
+									data = notFound + ' ' + errorCodes[notFound]
  2472
+
  2473
+								return res.send(data, notFound)
  2474
+							else
  2475
+								return res.send(notFound)
  2476
+
  2477
+						# 500 Middleware
  2478
+						server.use (err, req, res, next) ->
  2479
+							database = docpad.getDatabase()
  2480
+							return next() unless database
  2481
+							serverError = 500
  2482
+							if config.useCustomErrors
  2483
+								file = database.findOne(relativePath: '404.html')
  2484
+								if file
  2485
+									data = file.get('contentRendered') or document.get('content')
  2486
+								else
  2487
+									data = serverError + ' ' + errorCodes[serverError]
  2488
+
  2489
+								return res.send(data, serverError)
  2490
+							else
  2491
+								res.send(serverError)
2433 2492
 
2434 2493
 				# Start the Server
2435 2494
 				startServer()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.