Skip to content
This repository

Made redirect_handler_class/error_handler_class configurable. #467

Closed
wants to merge 1 commit into from

2 participants

Brian E. Granger Ben Darnell
Brian E. Granger

There are two new fields in Application.settings that are used in
tornado.web for the redirect/error handler classes. These default
to web.RedirectHandler and web.ErrorHandler. These are now handled
in the same was the static_handler_class is. This allows for
subclasses of Application and RequestHandler to customize this
logic more easily.

Brian E. Granger Made redirect_handler_class/error_handler_class configurable.
There are two new fields in Application.settings that are used in
tornado.web for the redirect/error handler classes. These default
to web.RedirectHandler and web.ErrorHandler. These are now handled
in the same was the static_handler_class is. This allows for
subclasses of Application and RequestHandler to customize this
logic more easily.
40a8ddd
Brian E. Granger

This PR was originally created to enable some custom PyZMQ request handlers to work without monkey patching. Those classes in PyZMQ have been refactored so this PR is no longer needed for that purpose. Should we close this PR, or do you still want to consider it. If there is still interest in merging this, let me know - I found a few more places that used hardcoded classes in tornado.web.

Ben Darnell
Collaborator

RedirectHandler is primarily instantiated by user code in the handler list; the one case where Tornado creates its own RedirectHandler is fairly esoteric, and if you want to customize that behavior you're probably going to want to add a wildcard url spec with your own handler instead of plugging in a variant of RedirectHandler.

Overriding ErrorHandler in this way makes more sense since many people have a hard time figuring out that you create a custom 404 page just by adding a wildcard handler at the end of the list. However, I think I'm going to solve that a bit differently by delegating default error handling to the Application (like logging is now), instead of making a new special case for 404s.

Ben Darnell bdarnell closed this April 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 22, 2012
Brian E. Granger Made redirect_handler_class/error_handler_class configurable.
There are two new fields in Application.settings that are used in
tornado.web for the redirect/error handler classes. These default
to web.RedirectHandler and web.ErrorHandler. These are now handled
in the same was the static_handler_class is. This allows for
subclasses of Application and RequestHandler to customize this
logic more easily.
40a8ddd
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 6 additions and 2 deletions. Show diff stats Hide diff stats

  1. 8  tornado/web.py
8  tornado/web.py
@@ -1336,8 +1336,10 @@ def __call__(self, request):
1336 1336
         args = []
1337 1337
         kwargs = {}
1338 1338
         handlers = self._get_host_handlers(request)
  1339
+        redirect_handler_class = self.settings.get("redirect_handler_class",
  1340
+                                            RedirectHandler)
1339 1341
         if not handlers:
1340  
-            handler = RedirectHandler(
  1342
+            handler = redirect_handler_class(
1341 1343
                 self, request, url="http://" + self.default_host + "/")
1342 1344
         else:
1343 1345
             for spec in handlers:
@@ -1365,7 +1367,9 @@ def unquote(s):
1365 1367
                             args = [unquote(s) for s in match.groups()]
1366 1368
                     break
1367 1369
             if not handler:
1368  
-                handler = ErrorHandler(self, request, status_code=404)
  1370
+                error_handler_class = self.settings.get("error_handler_class",
  1371
+                                                    ErrorHandler)
  1372
+                handler = error_handler_class(self, request, status_code=404)
1369 1373
 
1370 1374
         # In debug mode, re-compile templates and reload static files on every
1371 1375
         # request so you don't need to restart to see changes
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.