Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 650

Show
Ignore:
Timestamp:
09/14/05 16:34:40
Author:
mikerobi
Message:

implimented changes needed to re-close ticket:288

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cperror.py

    r645 r650  
    2828 
    2929import urllib 
     30import os 
    3031 
    3132 
     
    185186        if status < 400 or status > 599: 
    186187            raise ValueError("status must be between 400 and 599.") 
    187          
    188         # these 4 lines might dissapear 
    189         import cherrypy 
    190         self.statusString = cherrypy._cputil.getErrorStatusAndPage(status)[0] 
    191         cherrypy.response.status = self.statusString 
    192  
    193         if body is _missing: 
    194             # because the init method is called before the exception 
    195             # is raised it is impossible to embed the traceback in the 
    196             # error page at this point. We use a generator so that the 
    197             # error page is generated at a later point (after the 
    198             # exception is raised). 
    199             body = self.pageGenerator() 
    200          
    201         cherrypy.response.body = body 
     188          
     189        self.body = body 
     190     
     191    def set_response(self): 
     192        import cherrypy 
     193         
     194        # we now now have access to the traceback  
     195        statusString, defaultBody = cherrypy._cputil.getErrorStatusAndPage(self.status) 
     196         
     197        if self.body is _missing: 
     198            self.body = defaultBody 
     199            # try to look up a custom error page in the config map 
     200            # if there is no error page then use the pageGenerator 
     201             
     202            # The page generator is used because the init method is called  
     203            # before the exception is raised.  It is impossible to embed the 
     204            # traceback in the error page at this piont so we use the generator 
     205            # to render the error page at a later point 
     206             
     207            import cherrypy 
     208            # try and read the page from a file 
     209            # we use the default if the page can't be read 
     210            try: 
     211                errorPageFile = cherrypy.config.get('errorPage.%s' % status, '') 
     212                self.body = file(errorPageFile, 'r') 
     213            except: 
     214                # we have alread set the body 
     215                pass 
     216 
     217        cherrypy.response.status = statusString 
     218        cherrypy.response.body   = self.body 
    202219     
    203220    def __str__(self): 
    204         return self.statusString 
    205  
    206     def pageGenerator(self): 
    207         import cherrypy 
    208         yield cherrypy._cputil.getErrorStatusAndPage(self.status)[1] 
     221        import cherrypy 
     222        return cherrypy._cputil.getErrorStatusAndPage(self.status)[0] 
    209223 
    210224class NotFound(HTTPError): 
     
    214228        self.args = (path,) 
    215229        HTTPError.__init__(self, 404) 
     230 
     231    def __str__(self): 
     232        return self.args[0] 
  • trunk/cherrypy/_cphttptools.py

    r643 r650  
    286286                    applyFilters('beforeFinalize') 
    287287                    finalize() 
     288                except cherrypy.HTTPError, inst: 
     289                    # This includes NotFound 
     290                    inst.set_response() 
     291                    applyFilters('beforeFinalize') 
     292                    finalize() 
     293 
    288294            finally: 
    289295                applyFilters('onEndResource') 
     
    423429        applyFilters('beforeErrorResponse') 
    424430        
    425         # status, body may already be set by HTTPError constructor 
    426         if not isinstance(exc, cherrypy.HTTPError): 
    427             # _cpOnError will probably change cherrypy.response.body. 
    428             # It may also change the headerMap, etc. 
    429             _cputil.getSpecialAttribute('_cpOnError')() 
     431        # _cpOnError will probably change cherrypy.response.body. 
     432        # It may also change the headerMap, etc. 
     433        _cputil.getSpecialAttribute('_cpOnError')() 
    430434         
    431435        finalize() 
  • trunk/cherrypy/tutorial/tut10_http_errors.py

    r639 r650  
    33import cherrypy 
    44 
     5# we want to customize 403 errors 
     6customErrors = { 
     7                 'errorPage.403' : "custom_error.html" 
     8               } 
     9 
     10cherrypy.config.update({'/' : customErrors}) 
    511 
    612class HTTPErrorDemo(object): 
     
    1622        return """ 
    1723        <html><body> 
    18             <a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/
     24            <h2><a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/></h2
    1925            <a href="/doesNotExist">Click me i'm a broken link!</a> 
    20             <br/> 
    21             <a href="/customMessage">Use a custom error message</a> 
     26            <br/><br/> 
     27            <a href="/error?code=403">Use a custom an error page from a file.</a> 
    2228            <br/><br/> 
    2329            These errors are explicitly raised by the application. 
     
    2632            <a href="/error?code=402">402</a> 
    2733            <a href="/error?code=500">500</a> 
     34            <br/><br/> 
     35            <a href="/bodyArg">You can also set the response body when you raise an error</a> 
    2836        </body></html> 
    2937        """ % trace 
     
    4553    error.exposed = True 
    4654 
    47     def customMessage(self): 
    48         raise cherrypy.HTTPError(500, "Plain text message") 
    49     customMessage.exposed = True 
     55    def bodyArg(self): 
     56        message = """ If you construct a HTTPError wiht body argument, the body argument 
     57                      will overide any default or custom error page. 
     58                  """ 
     59        raise cherrypy.HTTPError(403, body = message) 
     60    bodyArg.exposed = True 
    5061 
    5162cherrypy.root = HTTPErrorDemo() 
  • trunk/cherrypy/tutorial/tutorial.conf

    r608 r650  
    33server.threadPool = 10 
    44server.environment = "production" 
    5 server.showTracebacks = True 
     5# server.showTracebacks = True 
    66# server.logToScreen = False 
  • trunk/docs/book/xml/apireference.xml

    r608 r650  
    299299      <listitem> 
    300300        <section> 
    301             <title>cherrypy.HTTPStatusError</title> 
     301            <title>cherrypy.HTTPError</title> 
    302302            <para> 
    303303                This exception can be used to automatically send a response  
  • trunk/docs/book/xml/appdeveloperreference.xml

    r633 r650  
    1818    <xi:include href="staticcontenthandling.xml" /> 
    1919    <xi:include href="fileuploadbehavior.xml" /> 
     20    <xi:include href="errorhandling.xml" /> 
    2021</section> 
  • trunk/setup.py

    r646 r650  
    4242            'cherrypy/tutorial/tutorial.conf', 
    4343            'cherrypy/tutorial/README.txt', 
     44            'cherrypy/tutorial/ReturnVsYield.txt', 
     45            'cherrypy/tutorial/custom_error.html', 
    4446        ] 
    4547    ), 

Hosted by WebFaction

Log in as guest/cpguest to create tickets