Changeset 606
- Timestamp:
- 09/04/05 16:47:18
- Files:
-
- trunk/cherrypy/_cperror.py (modified) (1 diff)
- trunk/cherrypy/_cphttptools.py (modified) (2 diffs)
- trunk/cherrypy/_cputil.py (modified) (3 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut10_http_errors.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cperror.py
r605 r606 176 176 """Exception raised when the client has made an error in its request.""" 177 177 178 def __init__(self, status=400, body=_missing):178 def __init__(self, status=400, message=None): 179 179 self.status = status = int(status) 180 180 if status < 400 or status > 499: 181 181 raise ValueError("status must be between 400 and 499.") 182 182 183 # these 2 lines might dissapear 183 184 import cherrypy 184 185 cherrypy.response.status = status 185 if body is not _missing:186 cherrypy.response.body = body187 186 188 self.message = body187 self.message = message 189 188 190 189 def getArgs(self): trunk/cherrypy/_cphttptools.py
r605 r606 450 450 451 451 try: 452 # try to return a nice error page if we are in production mode and http errors 453 # are enabled 452 454 isProduction = cherrypy.config.get('server.environment') == 'production' 453 455 httpErrors = cherrypy.config.get('server.httpErrors') … … 463 465 raise Exception() 464 466 except: 467 # if there was a problem generating the error page 468 # then return a basic message 465 469 body = "Unrecoverable error in the server." 466 470 if extrabody is not None: trunk/cherrypy/_cputil.py
r605 r606 156 156 """ Default _cpOnHTTPError method """ 157 157 status, customMessage = sys.exc_info()[1].getArgs() 158 158 159 # get the error mage 159 160 page = httperrors.getErrorPage(status, customMessage = customMessage) 160 161 cherrypy.response.status, cherrypy.response.body = page … … 171 172 def _cpOnError(): 172 173 """ 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') 176 177 177 178 response = cherrypy.response 178 179 179 180 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 180 183 if response.status == 404: 181 184 response.status, response.body = httperrors.getErrorPage(404) … … 183 186 response.status, response.body = httperrors.getErrorPage(500) 184 187 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' 187 192 else: 188 if cherrypy.config.get('showTraceBacks', False):189 cherrypy190 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' 192 197 193 198 if cherrypy.response.headerMap.has_key('Content-Encoding'): trunk/cherrypy/lib/cptools.py
r580 r606 205 205 if result == []: 206 206 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) 209 209 210 210 return result trunk/cherrypy/tutorial/tut10_http_errors.py
r605 r606 7 7 8 8 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 9 16 return """ 10 17 <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. 12 22 <a href="/error?code=400">400</a> 13 23 <a href="/error?code=401">401</a> 14 24 <a href="/error?code=402">402</a> 15 <a href="/error?code= 404">404</a>25 <a href="/error?code=500">500</a> 16 26 </body></html> 17 """ 27 """ % trace 18 28 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 19 38 20 39 def error(self, code): 40 # raise an error based on the get query 21 41 code = int(code) 22 42 HTTPClientError = cherrypy._cperror.HTTPClientError

