tut10_http_errors.py (2188B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\tutorial\tut10_http_errors.py 2 import os 3 localDir = os.path.dirname(__file__) 4 curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 5 import cherrypy 6 7 class HTTPErrorDemo(object): 8 _cp_config = {'error_page.403': os.path.join(curpath, 'custom_error.html')} 9 10 def index(self): 11 tracebacks = cherrypy.request.show_tracebacks 12 if tracebacks: 13 trace = 'off' 14 else: 15 trace = 'on' 16 return '\n <html><body>\n <p>Toggle tracebacks <a href="toggleTracebacks">%s</a></p>\n <p><a href="/doesNotExist">Click me; I\'m a broken link!</a></p>\n <p><a href="/error?code=403">Use a custom error page from a file.</a></p>\n <p>These errors are explicitly raised by the application:</p>\n <ul>\n <li><a href="/error?code=400">400</a></li>\n <li><a href="/error?code=401">401</a></li>\n <li><a href="/error?code=402">402</a></li>\n <li><a href="/error?code=500">500</a></li>\n </ul>\n <p><a href="/messageArg">You can also set the response body\n when you raise an error.</a></p>\n </body></html>\n ' % trace 17 18 index.exposed = True 19 20 def toggleTracebacks(self): 21 tracebacks = cherrypy.request.show_tracebacks 22 cherrypy.config.update({'request.show_tracebacks': not tracebacks}) 23 raise cherrypy.HTTPRedirect('/') 24 25 toggleTracebacks.exposed = True 26 27 def error(self, code): 28 raise cherrypy.HTTPError(status=code) 29 30 error.exposed = True 31 32 def messageArg(self): 33 message = "If you construct an HTTPError with a 'message' argument, it wil be placed on the error page (underneath the status line by default)." 34 raise cherrypy.HTTPError(500, message=message) 35 36 messageArg.exposed = True 37 38 39 import os.path 40 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 41 if __name__ == '__main__': 42 cherrypy.quickstart(HTTPErrorDemo(), config=tutconf) 43 else: 44 cherrypy.tree.mount(HTTPErrorDemo(), config=tutconf)