Changeset 650
- Timestamp:
- 09/14/05 16:34:40
- Files:
-
- trunk/cherrypy/_cperror.py (modified) (3 diffs)
- trunk/cherrypy/_cphttptools.py (modified) (2 diffs)
- trunk/cherrypy/tutorial/custom_error.html (added)
- trunk/cherrypy/tutorial/tut10_http_errors.py (modified) (4 diffs)
- trunk/cherrypy/tutorial/tutorial.conf (modified) (1 diff)
- trunk/docs/book/xml/apireference.xml (modified) (1 diff)
- trunk/docs/book/xml/appdeveloperreference.xml (modified) (1 diff)
- trunk/docs/book/xml/errorhandling.xml (added)
- trunk/setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cperror.py
r645 r650 28 28 29 29 import urllib 30 import os 30 31 31 32 … … 185 186 if status < 400 or status > 599: 186 187 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 202 219 203 220 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] 209 223 210 224 class NotFound(HTTPError): … … 214 228 self.args = (path,) 215 229 HTTPError.__init__(self, 404) 230 231 def __str__(self): 232 return self.args[0] trunk/cherrypy/_cphttptools.py
r643 r650 286 286 applyFilters('beforeFinalize') 287 287 finalize() 288 except cherrypy.HTTPError, inst: 289 # This includes NotFound 290 inst.set_response() 291 applyFilters('beforeFinalize') 292 finalize() 293 288 294 finally: 289 295 applyFilters('onEndResource') … … 423 429 applyFilters('beforeErrorResponse') 424 430 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')() 430 434 431 435 finalize() trunk/cherrypy/tutorial/tut10_http_errors.py
r639 r650 3 3 import cherrypy 4 4 5 # we want to customize 403 errors 6 customErrors = { 7 'errorPage.403' : "custom_error.html" 8 } 9 10 cherrypy.config.update({'/' : customErrors}) 5 11 6 12 class HTTPErrorDemo(object): … … 16 22 return """ 17 23 <html><body> 18 < a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/>24 <h2><a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/></h2> 19 25 <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> 22 28 <br/><br/> 23 29 These errors are explicitly raised by the application. … … 26 32 <a href="/error?code=402">402</a> 27 33 <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> 28 36 </body></html> 29 37 """ % trace … … 45 53 error.exposed = True 46 54 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 50 61 51 62 cherrypy.root = HTTPErrorDemo() trunk/cherrypy/tutorial/tutorial.conf
r608 r650 3 3 server.threadPool = 10 4 4 server.environment = "production" 5 server.showTracebacks = True5 # server.showTracebacks = True 6 6 # server.logToScreen = False trunk/docs/book/xml/apireference.xml
r608 r650 299 299 <listitem> 300 300 <section> 301 <title>cherrypy.HTTP StatusError</title>301 <title>cherrypy.HTTPError</title> 302 302 <para> 303 303 This exception can be used to automatically send a response trunk/docs/book/xml/appdeveloperreference.xml
r633 r650 18 18 <xi:include href="staticcontenthandling.xml" /> 19 19 <xi:include href="fileuploadbehavior.xml" /> 20 <xi:include href="errorhandling.xml" /> 20 21 </section> trunk/setup.py
r646 r650 42 42 'cherrypy/tutorial/tutorial.conf', 43 43 'cherrypy/tutorial/README.txt', 44 'cherrypy/tutorial/ReturnVsYield.txt', 45 'cherrypy/tutorial/custom_error.html', 44 46 ] 45 47 ),

