Commits

Robert Brewer  committed bd5410e

1. Fix for #305 (produce an error if a custom error page file can not be opened).

2. Also, a fix in _cputil.getErrorPage, where messages (and other kwargs set to None) were not printing defaults.

3. Removed defunct "server.httpErrors" config entry.

  • Participants
  • Parent commits a3bda71

Comments (0)

Files changed (3)

File cherrypy/_cputil.py

     """
     
     code, reason, message = cptools.validStatus(status)
-    errorPageFile = cherrypy.config.get('errorPage.%s' % code, '')
-    if errorPageFile:
-        template = file(errorPageFile, 'rb')
-    else:
-        template = _HTTPErrorTemplate
     
-    kwargs.setdefault('status', "%s %s" % (code, reason))
-    kwargs.setdefault('message', '')
-    kwargs.setdefault('traceback', '')
-    kwargs.setdefault('version', cherrypy.__version__)
+    # We can't use setdefault here, because some
+    # callers send None for kwarg values.
+    if kwargs.get('status') is None:
+        kwargs['status'] = "%s %s" % (code, reason)
+    if kwargs.get('message') is None:
+        kwargs['message'] = message
+    if kwargs.get('traceback') is None:
+        kwargs['traceback'] = ''
+    if kwargs.get('version') is None:
+        kwargs['version'] = cherrypy.__version__
     for k, v in kwargs.iteritems():
         if v is None:
             kwargs[k] = ""
     
+    template = _HTTPErrorTemplate
+    errorPageFile = cherrypy.config.get('errorPage.%s' % code, '')
+    if errorPageFile:
+        try:
+            template = file(errorPageFile, 'rb')
+        except:
+            m = kwargs['message']
+            if m:
+                m += "<br />"
+            m += ("In addition, the custom error page "
+                  "failed:\n<br />%s" % (sys.exc_info()[1]))
+            kwargs['message'] = m
+    
     return template % kwargs
 
 def formatExc(exc=None):

File cherrypy/config.py

     'server.socketQueueSize': 5,
     
     'server.environment': 'development',
-    'server.httpErrors' : True,
     'server.protocolVersion': 'HTTP/1.0',
     'server.logToScreen': True,
     'server.logFile': '',

File cherrypy/test/test_core.py

 
 class Error(Test):
     
+    def custom(self):
+        raise cherrypy.HTTPError(404)
+    
     def page_method(self):
         raise ValueError()
     
 
 cherrypy.config.update({
     'global': {'server.logToScreen': False,
-               'server.httpErrors' : False,
                'server.environment': 'production',
                'server.showTracebacks': True,
                'server.protocolVersion': "HTTP/1.1",
     '/error/cause_err_in_finalize': {
         'server.showTracebacks': False,
     },
+    '/error/custom': {
+        'errorPage.404': "nonexistent.html",
+    },
 })
 
 # Shortcut syntax--should get put in the "global" bucket
             self.assertErrorPage(500, msg, None)
         finally:
             ignore.pop()
+        
+        # Test error in custom error page (ticket #305).
+        self.getPage("/error/custom")
+        self.assertStatus("404 Not Found")
+        msg = ("Nothing matches the given URI<br />"
+               "In addition, the custom error page failed:\n<br />"
+               "[Errno 2] No such file or directory: 'nonexistent.html'")
+        self.assertInBody(msg)
+
     
     def test_Ranges(self):
         self.getPage("/ranges/get_ranges", [('Range', 'bytes=3-6')])