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

Changeset 606

Show
Ignore:
Timestamp:
09/04/05 16:47:18
Author:
mikerobi
Message:

improvments to the http errors tutorial (10), minor cleanups of the http error code.

Files:

Legend:

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

    r605 r606  
    176176    """Exception raised when the client has made an error in its request.""" 
    177177     
    178     def __init__(self, status=400, body=_missing): 
     178    def __init__(self, status=400, message=None): 
    179179        self.status = status = int(status) 
    180180        if status < 400 or status > 499: 
    181181            raise ValueError("status must be between 400 and 499.") 
    182182         
     183        # these 2 lines might dissapear 
    183184        import cherrypy 
    184185        cherrypy.response.status = status 
    185         if body is not _missing: 
    186             cherrypy.response.body = body 
    187186         
    188         self.message = body 
     187        self.message = message 
    189188     
    190189    def getArgs(self): 
  • trunk/cherrypy/_cphttptools.py

    r605 r606  
    450450     
    451451    try: 
     452        # try to return a nice error page if we are in production mode and http errors 
     453        # are enabled 
    452454        isProduction = cherrypy.config.get('server.environment') == 'production' 
    453455        httpErrors = cherrypy.config.get('server.httpErrors') 
     
    463465            raise Exception() 
    464466    except: 
     467        # if there was a problem generating the error page 
     468        # then return a basic message 
    465469        body = "Unrecoverable error in the server." 
    466470        if extrabody is not None: 
  • trunk/cherrypy/_cputil.py

    r605 r606  
    156156    """ Default _cpOnHTTPError method """ 
    157157    status, customMessage = sys.exc_info()[1].getArgs() 
    158      
     158    
     159    # get the error mage 
    159160    page = httperrors.getErrorPage(status, customMessage = customMessage) 
    160161    cherrypy.response.status, cherrypy.response.body = page 
     
    171172def _cpOnError(): 
    172173    """ Default _cpOnError method """ 
    173     developmentMode = cherrypy.config.get('server.environment') == 'development' 
    174     httpErrors     = cherrypy.config.get('server.httpErrors') 
    175     showTracebacks = cherrypy.config.get('server.showTracebacks') 
     174    developmentMode = cherrypy.config.get('server.environment') == 'development' 
     175    httpErrors      = cherrypy.config.get('server.httpErrors') 
     176    showTracebacks = cherrypy.config.get('server.showTracebacks') 
    176177     
    177178    response = cherrypy.response 
    178179     
    179180    if not developmentMode and httpErrors: 
     181        # if it isn't development mode and http errors are turned on 
     182        # set the respose status and render the body 
    180183        if response.status == 404: 
    181184            response.status, response.body = httperrors.getErrorPage(404) 
     
    183186            response.status, response.body = httperrors.getErrorPage(500) 
    184187    elif developmentMode or showTracebacks: 
    185         cherrypy.response.body = [formatExc()] 
    186         cherrypy.response.headerMap['Content-Type'] = 'text/plain' 
     188        # if it is development mode or tracebacks are turned on 
     189        # return the traceback as plaintext 
     190        response.body = [formatExc()] 
     191        response.headerMap['Content-Type'] = 'text/plain' 
    187192    else: 
    188         if cherrypy.config.get('showTraceBacks', False): 
    189             cherrypy 
    190         cherrypy.response.body = "Unrecoverable error in the server" 
    191         cherrypy.response.headerMap['Content-Type'] = 'text/plain' 
     193        # If it is production mode but http errors are disabled then 
     194        # Display a simple, generic error message 
     195        response.body = "Unrecoverable error in the server" 
     196        response.headerMap['Content-Type'] = 'text/plain' 
    192197     
    193198    if cherrypy.response.headerMap.has_key('Content-Encoding'): 
  • trunk/cherrypy/lib/cptools.py

    r580 r606  
    205205    if result == []: 
    206206        cherrypy.response.headerMap['Content-Range'] = "bytes */%s" % content_length 
    207         b = "Invalid Range (first-byte-pos greater than Content-Length)" 
    208         raise cherrypy.HTTPClientError(416, b
     207        message = "Invalid Range (first-byte-pos greater than Content-Length)" 
     208        raise cherrypy.HTTPClientError(416, message
    209209     
    210210    return result 
  • trunk/cherrypy/tutorial/tut10_http_errors.py

    r605 r606  
    77     
    88    def index(self): 
     9        # display some links that will result in errors 
     10        tracebacks = cherrypy.config.get('server.showTracebacks') 
     11        if tracebacks: 
     12            trace = 'off' 
     13        else: 
     14            trace = 'on' 
     15             
    916        return """ 
    1017        <html><body> 
    11             <p>Edit tutorial.conf to turn on tracebacks</p> 
     18            <a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/> 
     19            <a href="/doesNotExist">Click me i'm a broken link!</a> 
     20            <br/><br/> 
     21            These errors are explicitly raised by the application. 
    1222            <a href="/error?code=400">400</a> 
    1323            <a href="/error?code=401">401</a> 
    1424            <a href="/error?code=402">402</a> 
    15             <a href="/error?code=404">404</a> 
     25            <a href="/error?code=500">500</a> 
    1626        </body></html> 
    17         """ 
     27        """ % trace 
    1828    index.exposed = True 
     29 
     30    def toggleTracebacks(self): 
     31        # simple function to toggle tracebacks on and off  
     32        tracebacks = cherrypy.config.get('server.showTracebacks') 
     33        cherrypy.config.update({'server.showTracebacks': not tracebacks}) 
     34         
     35        # redirect back to the index 
     36        raise cherrypy._cperror.HTTPRedirect('/') 
     37    toggleTracebacks.exposed=True 
    1938     
    2039    def error(self, code): 
     40        # raise an error based on the get query 
    2141        code = int(code) 
    2242        HTTPClientError = cherrypy._cperror.HTTPClientError 

Hosted by WebFaction

Log in as guest/cpguest to create tickets