Commits

Robert Brewer committed 908992a

Fix for #208; HTTPRedirect can now be raised in error handlers.

Comments (0)

Files changed (2)

cherrypy/_cphttptools.py

         finalize()
         
         applyFilters('afterErrorResponse')
+        return
+    except cherrypy.HTTPRedirect, inst:
+        try:
+            inst.set_response()
+            finalize()
+            return
+        except:
+            # Fall through to the second error handler
+            pass
     except:
-        # Failure in _cpOnError, error filter, or finalize.
-        # Bypass them all.
-        defaultOn = (cherrypy.config.get('server.environment') == 'development')
-        if cherrypy.config.get('server.showTracebacks', defaultOn):
-            body = dbltrace % (_cputil.formatExc(exc), _cputil.formatExc())
-        else:
-            body = ""
-        response = cherrypy.response
-        response.status, response.headers, response.body = bareError(body)
-
+        # Fall through to the second error handler
+        pass
+    
+    # Failure in _cpOnError, error filter, or finalize.
+    # Bypass them all.
+    defaultOn = (cherrypy.config.get('server.environment') == 'development')
+    if cherrypy.config.get('server.showTracebacks', defaultOn):
+        body = dbltrace % (_cputil.formatExc(exc), _cputil.formatExc())
+    else:
+        body = ""
+    response = cherrypy.response
+    response.status, response.headers, response.body = bareError(body)
 
 def bareError(extrabody=None):
     """Produce status, headers, body for a critical error.

cherrypy/test/test_core.py

 
 class Redirect(Test):
     
+    def _cpOnError(self):
+        raise cherrypy.HTTPRedirect("/errpage")
+    
+    def error(self):
+        raise NameError()
+    
     def index(self):
         return "child"
     
         self.getPage("/redirect/internal2?user_id=terrier")
         self.assertBody('0 images for fish')
         self.assertStatus('200 OK')
+        
+        # HTTPRedirect on error
+        self.getPage("/redirect/error")
+        self.assertStatus('303 See Other')
+        self.assertInBody('/errpage')
     
     def testFlatten(self):
         for url in ["/flatten/as_string", "/flatten/as_list",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.