| 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 os |
|---|
| 12 |
localDir = os.path.dirname(__file__) |
|---|
| 13 |
curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) |
|---|
| 14 |
|
|---|
| 15 |
import cherrypy |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class HTTPErrorDemo(object): |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
_cp_config = {'error_page.403' : os.path.join(curpath, "custom_error.html")} |
|---|
| 22 |
|
|---|
| 23 |
def index(self): |
|---|
| 24 |
|
|---|
| 25 |
tracebacks = cherrypy.request.show_tracebacks |
|---|
| 26 |
if tracebacks: |
|---|
| 27 |
trace = 'off' |
|---|
| 28 |
else: |
|---|
| 29 |
trace = 'on' |
|---|
| 30 |
|
|---|
| 31 |
return """ |
|---|
| 32 |
<html><body> |
|---|
| 33 |
<h2><a href="toggleTracebacks">Toggle tracebacks %s</a></h2> |
|---|
| 34 |
<p><a href="/doesNotExist">Click me; I'm a broken link!</a></p> |
|---|
| 35 |
<p><a href="/error?code=403">Use a custom an error page from a file.</a></p> |
|---|
| 36 |
<p>These errors are explicitly raised by the application:</p> |
|---|
| 37 |
<ul> |
|---|
| 38 |
<li><a href="/error?code=400">400</a></li> |
|---|
| 39 |
<li><a href="/error?code=401">401</a></li> |
|---|
| 40 |
<li><a href="/error?code=402">402</a></li> |
|---|
| 41 |
<li><a href="/error?code=500">500</a></li> |
|---|
| 42 |
</ul> |
|---|
| 43 |
<p><a href="/messageArg">You can also set the response body |
|---|
| 44 |
when you raise an error.</a></p> |
|---|
| 45 |
</body></html> |
|---|
| 46 |
""" % trace |
|---|
| 47 |
index.exposed = True |
|---|
| 48 |
|
|---|
| 49 |
def toggleTracebacks(self): |
|---|
| 50 |
|
|---|
| 51 |
tracebacks = cherrypy.request.show_tracebacks |
|---|
| 52 |
cherrypy.config.update({'request.show_tracebacks': not tracebacks}) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
raise cherrypy.HTTPRedirect('/') |
|---|
| 56 |
toggleTracebacks.exposed = True |
|---|
| 57 |
|
|---|
| 58 |
def error(self, code): |
|---|
| 59 |
|
|---|
| 60 |
raise cherrypy.HTTPError(status = code) |
|---|
| 61 |
error.exposed = True |
|---|
| 62 |
|
|---|
| 63 |
def messageArg(self): |
|---|
| 64 |
message = ("If you construct an HTTPError with a 'message' " |
|---|
| 65 |
"argument, it wil be placed on the error page " |
|---|
| 66 |
"(underneath the status line by default).") |
|---|
| 67 |
raise cherrypy.HTTPError(500, message=message) |
|---|
| 68 |
messageArg.exposed = True |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
cherrypy.tree.mount(HTTPErrorDemo()) |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
if __name__ == '__main__': |
|---|
| 75 |
import os.path |
|---|
| 76 |
thisdir = os.path.dirname(__file__) |
|---|
| 77 |
cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf')) |
|---|