| 1 |
<?xml version='1.0'?> |
|---|
| 2 |
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5b1//EN" "http://www.oasis-open.org/docbook/xml/4.5b1/docbookx.dtd"> |
|---|
| 3 |
<section xml:id="globaloverviewcherrypy" xmlns:db="http://docbook.org/docbook-ng" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
|---|
| 4 |
|
|---|
| 5 |
<title>Exceptions and Error Handling</title> |
|---|
| 6 |
|
|---|
| 7 |
<para>As you read this section, refer to the following diagram to |
|---|
| 8 |
understand the flow of execution:</para> |
|---|
| 9 |
|
|---|
| 10 |
<figure><title>Error flow execution</title><mediaobject><imageobject> |
|---|
| 11 |
<imagedata fileref="errors.gif"/></imageobject></mediaobject></figure> |
|---|
| 12 |
|
|---|
| 13 |
<section> |
|---|
| 14 |
|
|---|
| 15 |
<title>Unanticipated exceptions</title> |
|---|
| 16 |
|
|---|
| 17 |
<para>When an unhandled exception is raised inside CherryPy, three |
|---|
| 18 |
actions occur (in order):</para> |
|---|
| 19 |
|
|---|
| 20 |
<itemizedlist> |
|---|
| 21 |
|
|---|
| 22 |
<listitem> |
|---|
| 23 |
|
|---|
| 24 |
<para><code>before_error_response</code> filter methods are |
|---|
| 25 |
called</para> |
|---|
| 26 |
|
|---|
| 27 |
</listitem> |
|---|
| 28 |
|
|---|
| 29 |
<listitem> |
|---|
| 30 |
|
|---|
| 31 |
<para>a <code>_cp_on_error</code> method is called</para> |
|---|
| 32 |
|
|---|
| 33 |
</listitem> |
|---|
| 34 |
|
|---|
| 35 |
<listitem> |
|---|
| 36 |
|
|---|
| 37 |
<para><code>response.finalize</code> is called</para> |
|---|
| 38 |
|
|---|
| 39 |
</listitem> |
|---|
| 40 |
|
|---|
| 41 |
<listitem> |
|---|
| 42 |
|
|---|
| 43 |
<para><code>after_error_response</code> filter methods are |
|---|
| 44 |
called</para> |
|---|
| 45 |
|
|---|
| 46 |
</listitem> |
|---|
| 47 |
|
|---|
| 48 |
</itemizedlist> |
|---|
| 49 |
|
|---|
| 50 |
<para>The error response filter methods are defined by each filter; |
|---|
| 51 |
they cannot prevent the call to <code>_cp_on_error</code> (unless |
|---|
| 52 |
<code>before_error_response</code> raises an exception, including |
|---|
| 53 |
HTTPRedirect).</para> |
|---|
| 54 |
|
|---|
| 55 |
<para>The <code>_cp_on_error</code> function is a CherryPy |
|---|
| 56 |
"special attribute"; that is, you can define your own |
|---|
| 57 |
<code>_cp_on_error</code> method for any branch in your |
|---|
| 58 |
<code>cherrypy.root</code> object tree, and it will be invoked for |
|---|
| 59 |
all child handlers. For example:</para> |
|---|
| 60 |
|
|---|
| 61 |
<example> |
|---|
| 62 |
|
|---|
| 63 |
<title>A custom <code>_cp_on_error</code> method</title> |
|---|
| 64 |
|
|---|
| 65 |
<programlisting>import cherrypy |
|---|
| 66 |
|
|---|
| 67 |
class Root: |
|---|
| 68 |
|
|---|
| 69 |
def _cp_on_error(self): |
|---|
| 70 |
cherrypy.response.body = ("We apologise for the fault in the website. " |
|---|
| 71 |
"Those responsible have been sacked.") |
|---|
| 72 |
|
|---|
| 73 |
def index(self): |
|---|
| 74 |
return "A m" + 00 + "se once bit my sister..." |
|---|
| 75 |
index.exposed = True</programlisting> |
|---|
| 76 |
</example> |
|---|
| 77 |
|
|---|
| 78 |
<para>The default <code>_cp_on_error</code> function simply responds |
|---|
| 79 |
as if an HTTPError 500 had been raised (see the next |
|---|
| 80 |
section).</para> |
|---|
| 81 |
|
|---|
| 82 |
<para>If an HTTPRedirect is raised during the error-handling |
|---|
| 83 |
process, it will be handled appropriately. If any other kind of |
|---|
| 84 |
error occurs during the handling of an initial error, then CherryPy |
|---|
| 85 |
punts, returning a bare-bones, <code>text/plain</code> error |
|---|
| 86 |
response (containing both tracebacks if |
|---|
| 87 |
<code>server.show_tracebacks</code> is True).</para> |
|---|
| 88 |
|
|---|
| 89 |
</section> |
|---|
| 90 |
|
|---|
| 91 |
<section> |
|---|
| 92 |
|
|---|
| 93 |
<title>HTTPError</title> |
|---|
| 94 |
|
|---|
| 95 |
<para>HTTPError exceptions do not result in calls to |
|---|
| 96 |
<code>_cp_on_error</code>. Instead, they have their own |
|---|
| 97 |
<code>_cp_on_http_error</code> function. Like <code>_cp_on_error</code>, |
|---|
| 98 |
this is a "special attribute" and can be overridden by |
|---|
| 99 |
cherrypy.root objects. The default <code>_cp_on_http_error</code> |
|---|
| 100 |
handler sets the HTTP response to a pretty HTML error page.</para> |
|---|
| 101 |
|
|---|
| 102 |
</section> |
|---|
| 103 |
|
|---|
| 104 |
<section> |
|---|
| 105 |
|
|---|
| 106 |
<title>HTTPRedirect</title> |
|---|
| 107 |
|
|---|
| 108 |
<para>HTTPRedirect exceptions are not errors; therefore, there is |
|---|
| 109 |
no way to override their behavior. They set the response to an |
|---|
| 110 |
appropriate status, header set, and body, according to the HTTP |
|---|
| 111 |
spec.</para> |
|---|
| 112 |
|
|---|
| 113 |
</section> |
|---|
| 114 |
|
|---|
| 115 |
</section> |
|---|
| 116 |
|
|---|