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

root/branches/cherrypy-2.x/cherrypy/tutorial/tut10_http_errors.py

Revision 856 (checked in by rdelon, 3 years ago)

Big change: camelCase to lower_with_underscore names (still need to update the book)

  • Property svn:eol-style set to native
Line 
1 """
2
3 Tutorial: HTTP errors
4
5 HTTPError is used to return an error response to the client.
6 CherryPy has lots of options regarding how such errors are
7 logged, displayed, and formatted.
8
9 """
10
11 import cherrypy
12
13
14 class HTTPErrorDemo(object):
15    
16     def index(self):
17         # display some links that will result in errors
18         tracebacks = cherrypy.config.get('server.show_tracebacks')
19         if tracebacks:
20             trace = 'off'
21         else:
22             trace = 'on'
23            
24         return """
25         <html><body>
26             <h2><a href="toggleTracebacks">Toggle tracebacks %s</a></h2>
27             <p><a href="/doesNotExist">Click me; I'm a broken link!</a></p>
28             <p><a href="/error?code=403">Use a custom an error page from a file.</a></p>
29             <p>These errors are explicitly raised by the application:</p>
30             <ul>
31                 <li><a href="/error?code=400">400</a></li>
32                 <li><a href="/error?code=401">401</a></li>
33                 <li><a href="/error?code=402">402</a></li>
34                 <li><a href="/error?code=500">500</a></li>
35             </ul>
36             <p><a href="/messageArg">You can also set the response body
37             when you raise an error.</a></p>
38         </body></html>
39         """ % trace
40     index.exposed = True
41    
42     def toggleTracebacks(self):
43         # simple function to toggle tracebacks on and off
44         tracebacks = cherrypy.config.get('server.show_tracebacks')
45         cherrypy.config.update({'server.show_tracebacks': not tracebacks})
46        
47         # redirect back to the index
48         raise cherrypy.HTTPRedirect('/')
49     toggleTracebacks.exposed = True
50    
51     def error(self, code):
52         # raise an error based on the get query
53         raise cherrypy.HTTPError(status = code)
54     error.exposed = True
55    
56     def messageArg(self):
57         message = ("If you construct an HTTPError with a 'message' "
58                    "argument, it wil be placed on the error page "
59                    "(underneath the status line by default).")
60         raise cherrypy.HTTPError(500, message=message)
61     messageArg.exposed = True
62
63
64 cherrypy.root = HTTPErrorDemo()
65
66 # Set a custom response for 403 errors.
67 import os
68 localDir = os.path.dirname(__file__)
69 curpath = os.path.normpath(os.path.join(os.getcwd(), localDir))
70 cherrypy.config.update({'error_page.403' : os.path.join(curpath, "custom_error.html")})
71
72
73 if __name__ == '__main__':
74     # Use the configuration file tutorial.conf.
75     cherrypy.config.update(file = 'tutorial.conf')
76     # Start the CherryPy server.
77     cherrypy.server.start()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets