 
 						# 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]
 


						# 404 Middleware
						server.use (req, res, next) ->
							notFound = 404
							if config.useCustomErrors
								code = findClosest(notFound)
								if errorFiles.hasOwnProperty(code) && pathUtil.existsSync errorFiles[code]
									data = fsUtil.readFileSync(errorFiles[code], 'utf8')
								else
									data = notFound + ' ' + errorCodes[notFound]


		config = @config
		server = null

		# Handlers
		complete = (err) ->
			return next(err)  if err
