Flag for bypassing default error pages #251

Merged
merged 4 commits into from Jul 19, 2012
View
@@ -3,6 +3,7 @@
# Necessary
pathUtil = require('path')
+fsUtil = require('fs')
_ = require('underscore')
caterpillar = require('caterpillar')
CSON = require('cson')
@@ -104,6 +105,32 @@ class DocPad extends EventEmitterEnhanced
getEvents: ->
@events
+ errors:
+ '400': 'Bad Request'
+ '401': 'Unauthorized'
+ '402': 'Payment Required'
+ '403': 'Forbidden'
+ '404': 'Not Found'
+ '405': 'Method Not Allowed'
+ '406': 'Not Acceptable'
+ '407': 'Proxy Authentication Required'
+ '408': 'Request Timeout'
+ '409': 'Conflict'
+ '410': 'Gone'
+ '411': 'Length Required'
+ '412': 'Precondition Failed'
+ '413': 'Request Entity Too Large'
+ '414': 'Request-URI Too Long'
+ '415': 'Unsupported Media Type'
+ '416': 'Requested Range Not Satisfiable'
+ '417': 'Expectation Failed'
+ '500': 'Internal Server Error'
+ '501': 'Not Implemented'
+ '502': 'Bad Gateway'
+ '503': 'Service Unavailable'
+ '504': 'Gateway Timeout'
+ '505': 'HTTP Version Not Supported'
+
# ---------------------------------
# Collections
@@ -329,6 +356,10 @@ class DocPad extends EventEmitterEnhanced
# Whether or not we should extend the server with extra middleware and routing
extendServer: true
+ # Enable Custom Error Pages
+ # A flag to provide an entry to handle custom error pages
+ useCustomErrors: false
+
# Port
# The port that the server should use
port: 9778
@@ -2412,7 +2443,7 @@ class DocPad extends EventEmitterEnhanced
contentRendered = document.get('contentRendered')
if err
docpad.error(err)
- return res.send(err.message, 500)
+ return next(err)
else
return res.send(contentRendered)
else
@@ -2428,8 +2459,36 @@ class DocPad extends EventEmitterEnhanced
server.use(express.static config.outPath)
# 404 Middleware
- server.use (req,res,next) ->
- return res.send(404)
+ server.use (req, res, next) ->
+ database = docpad.getDatabase()
+ return next() unless database
+ notFound = 404
+ if config.useCustomErrors
+ file = database.findOne(relativePath: '404.html')
+ if file
+ data = file.get('contentRendered') or document.get('content')
+ else
+ data = notFound + ' ' + errorCodes[notFound]
+
+ return res.send(data, notFound)
+ else
+ return res.send(notFound)
+
+ # 500 Middleware
+ server.use (err, req, res, next) ->
+ database = docpad.getDatabase()
+ return next() unless database
+ serverError = 500
+ if config.useCustomErrors
+ file = database.findOne(relativePath: '404.html')
+ if file
+ data = file.get('contentRendered') or document.get('content')
+ else
+ data = serverError + ' ' + errorCodes[serverError]
+
+ return res.send(data, serverError)
+ else
+ res.send(serverError)
# Start the Server
startServer()