Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Flag for bypassing default error pages #251

Merged
merged 4 commits into from

2 participants

@ncrohn

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.

@balupton
Owner

Sweet, good work!

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

@ncrohn

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.

@ncrohn

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.

@ncrohn

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

@balupton
Owner

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

@ncrohn

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!

@balupton
Owner

Doing it now :)

@balupton balupton referenced this pull request from a commit
@balupton balupton 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
@balupton balupton merged commit 09de510 into from
@balupton
Owner

Published in v6.4.0

@balupton
Owner

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

:D

@ncrohn

Awesome! Thanks for getting this in!

@balupton balupton referenced this pull request from a commit
@balupton balupton 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
Commits on Jun 28, 2012
  1. Added flag for custom error pages

    ncrohn authored
Commits on Jul 2, 2012
  1. Movde errors plugin to core docpad code

    ncrohn authored
  2. Re-added custom flag - defaults to false

    ncrohn authored
Commits on Jul 9, 2012
This page is out of date. Refresh to see the latest.
Showing with 62 additions and 3 deletions.
  1. +62 −3 src/lib/docpad.coffee
View
65 src/lib/docpad.coffee
@@ -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()
Something went wrong with that request. Please try again.