Robert Brewer avatar Robert Brewer committed 110222e

Moved IndexRedirect into cperror. Application code may now raise it as desired.

Comments (0)

Files changed (2)

cherrypy/_cphttptools.py

     path = urllib.unquote(path)
     
     try:
-        func, objectPathList, virtualPathList = mapPathToObject()
-    except IndexRedirect, inst:
+        func, objectPathList, virtualPathList = mapPathToObject()
+        
+        # Remove "root" from objectPathList and join it to get objectPath
+        cpg.request.objectPath = '/' + '/'.join(objectPathList[1:])
+        body = func(*(virtualPathList + cpg.request.paramList),
+                    **(cpg.request.paramMap))
+        cpg.response.body = iterable(body)
+    except cperror.IndexRedirect, inst:
         # For an IndexRedirect, we don't go through the regular
         # mechanism: we return the redirect immediately
         newUrl = urlparse.urljoin(cpg.request.base, inst.args[0])
         cpg.response.status = '302 Found'
         cpg.response.headerMap['Location'] = newUrl
         cpg.response.body = []
-        return
-     
-    # Remove "root" from objectPathList and join it to get objectPath
-    cpg.request.objectPath = '/' + '/'.join(objectPathList[1:])
-    body = func(*(virtualPathList + cpg.request.paramList),
-                **(cpg.request.paramMap))
-    
-    cpg.response.body = body = iterable(body)
-    return body
 
 def iterable(body):
     # build a uniform return type (iterable)
 
 # Object lookup
 
-class IndexRedirect(Exception): pass
-
-
 def getObjFromPath(objPathList):
     """ For a given objectPathList (like ['root', 'a', 'b', 'index']),
          return the object (or None if it doesn't exist).
             newUrl = cpg.request.path + '/'
             if cpg.request.queryString:
                 newUrl += cpg.request.queryString
-            raise IndexRedirect(newUrl)
+            raise cperror.IndexRedirect(newUrl)
     
     return candidate, objectPathList, virtualPathList
 

cherrypy/cperror.py

 class RequestHandled(Exception):
     """Exception raised when no further request handling should occur."""
     pass
+
+class IndexRedirect(Exception):
+    """Exception raised when the request should be redirected.
+    
+    The new URL must be passed as the first argument to the Exception, e.g.,
+        cperror.IndexRedirect(newUrl)
+    """
+    pass
+
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.