Navigation Menu

Skip to content

Commit

Permalink
Updated to pull only 404 and 500 with using docpad internals
Browse files Browse the repository at this point in the history
  • Loading branch information
ncrohn committed Jul 9, 2012
1 parent 47b92c4 commit dc15e9a
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions src/lib/docpad.coffee
Expand Up @@ -2348,32 +2348,6 @@ class DocPad extends EventEmitterEnhanced
config = @config
server = null

# Error Handling Stuff
errorFiles = {}
# read the out directory for available files
fileList = fsUtil.readdirSync(config.outPath)

for file in fileList
if file.match /^[0-9x]{3}\.html/
code = file.match /^([0-9x]{3})\.html/
errorFiles[code[1]] = pathUtil.join(config.outPath, file)

# Find the closest matching error page
findClosest = (code) ->
code = code.toString()
match = null
count = 2

while match == null
if errorFiles.hasOwnProperty(code)
match = code
else
code = code.substr(0, count)
while code.length < 3
code += 'x'
count -= 1
return match

# Handlers
complete = (err) ->
return next(err) if err
Expand Down Expand Up @@ -2486,11 +2460,13 @@ class DocPad extends EventEmitterEnhanced

# 404 Middleware
server.use (req, res, next) ->
database = docpad.getDatabase()
return next() unless database
notFound = 404
if config.useCustomErrors
code = findClosest(notFound)
if errorFiles.hasOwnProperty(code) && pathUtil.existsSync errorFiles[code]
data = fsUtil.readFileSync(errorFiles[code], 'utf8')
file = database.findOne(relativePath: '404.html')
if file
data = file.get('contentRendered') or document.get('content')
else
data = notFound + ' ' + errorCodes[notFound]

Expand All @@ -2500,12 +2476,13 @@ class DocPad extends EventEmitterEnhanced

# 500 Middleware
server.use (err, req, res, next) ->
# Assume 500 for now
database = docpad.getDatabase()
return next() unless database
serverError = 500
if config.useCustomErrors
code = findClosest(serverError)
if errorFiles.hasOwnProperty(code) && pathUtil.existsSync errorFiles[code]
data = fsUtil.readFileSync(errorFiles[code], 'utf8')
file = database.findOne(relativePath: '404.html')
if file
data = file.get('contentRendered') or document.get('content')
else
data = serverError + ' ' + errorCodes[serverError]

Expand Down

0 comments on commit dc15e9a

Please sign in to comment.