| 1 | | <?xml version="1.0" encoding="UTF-8"?> |
|---|
| 2 | | <section xmlns:db="http://docbook.org/docbook-ng" xmlns:xi="http://www.w3.org/2001/XInclude" |
|---|
| 3 | | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml:id="globaloverviewcherrypy"> |
|---|
| 4 | | <title>Exceptions and Error Handling</title> |
|---|
| 5 | | <para>As you read this section, refer to the following diagram to understand the flow of |
|---|
| 6 | | execution:</para> |
|---|
| 7 | | <figure> |
|---|
| 8 | | <mediaobject> |
|---|
| 9 | | <imageobject> |
|---|
| 10 | | <imagedata fileref="errors.gif" /> |
|---|
| 11 | | </imageobject> |
|---|
| 12 | | </mediaobject> |
|---|
| 13 | | </figure> |
|---|
| 14 | | <section> |
|---|
| 15 | | <title>Unanticipated exceptions</title> |
|---|
| 16 | | <para>When an unhandled exception is raised inside CherryPy, three actions occur (in |
|---|
| 17 | | order):</para> |
|---|
| 18 | | <itemizedlist> |
|---|
| 19 | | <listitem> |
|---|
| 20 | | <para><code>beforeErrorResponse</code> filter methods are called</para> |
|---|
| 21 | | </listitem> |
|---|
| 22 | | <listitem> |
|---|
| 23 | | <para>a <code>_cpOnError</code> method is called</para> |
|---|
| 24 | | </listitem> |
|---|
| 25 | | <listitem> |
|---|
| 26 | | <para><code>response.finalize</code> is called</para> |
|---|
| 27 | | </listitem> |
|---|
| 28 | | <listitem> |
|---|
| 29 | | <para><code>afterErrorResponse</code> filter methods are called</para> |
|---|
| 30 | | </listitem> |
|---|
| 31 | | </itemizedlist> |
|---|
| 32 | | <para>The error response filter methods are defined by each filter; they cannot prevent the |
|---|
| 33 | | call to <code>_cpOnError</code> (unless <code>beforeErrorResponse</code> raises an exception, |
|---|
| 34 | | including HTTPRedirect).</para> |
|---|
| 35 | | <para>The <code>_cpOnError</code> function is a CherryPy "special attribute"; that is, you |
|---|
| 36 | | can define your own <code>_cpOnError</code> method for any branch in your |
|---|
| 37 | | <code>cherrypy.root</code> object tree, and it will be invoked for all child handlers. For |
|---|
| 38 | | example:</para> |
|---|
| 39 | | <example> |
|---|
| 40 | | <title>A custom <code>_cpOnError</code> method</title> |
|---|
| 41 | | <programlisting>import cherrypy |
|---|
| | 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>beforeErrorResponse</code> filter methods are |
|---|
| | 25 | called</para> |
|---|
| | 26 | |
|---|
| | 27 | </listitem> |
|---|
| | 28 | |
|---|
| | 29 | <listitem> |
|---|
| | 30 | |
|---|
| | 31 | <para>a <code>_cpOnError</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>afterErrorResponse</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>_cpOnError</code> (unless |
|---|
| | 52 | <code>beforeErrorResponse</code> raises an exception, including |
|---|
| | 53 | HTTPRedirect).</para> |
|---|
| | 54 | |
|---|
| | 55 | <para>The <code>_cpOnError</code> function is a CherryPy |
|---|
| | 56 | "special attribute"; that is, you can define your own |
|---|
| | 57 | <code>_cpOnError</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>_cpOnError</code> method</title> |
|---|
| | 64 | |
|---|
| | 65 | <programlisting>import cherrypy |
|---|
| 52 | | </example> |
|---|
| 53 | | <para>The default <code>_cpOnError</code> function simply responds as if an HTTPError 500 had |
|---|
| 54 | | been raised (see the next section).</para> |
|---|
| 55 | | <para>If an HTTPRedirect is raised during the error-handling process, it will be handled |
|---|
| 56 | | appropriately. If any other kind of error occurs during the handling of an initial error, |
|---|
| 57 | | then CherryPy punts, returning a bare-bones, <code>text/plain</code> error response |
|---|
| 58 | | (containing both tracebacks if <code>server.showTracebacks</code> is True).</para> |
|---|
| 59 | | </section> |
|---|
| 60 | | <section> |
|---|
| 61 | | <title>HTTPError</title> |
|---|
| 62 | | <para>HTTPError exceptions do not result in calls to <code>_cpOnError</code>. Instead, they |
|---|
| 63 | | have their own <code>_cpOnHTTPError</code> function. Like <code>_cpOnError</code>, this is a |
|---|
| 64 | | "special attribute" and can be overridden by cherrypy.root objects. The default |
|---|
| 65 | | <code>_cpOnHTTPError</code> handler sets the HTTP response to a pretty HTML error |
|---|
| 66 | | page.</para> |
|---|
| 67 | | </section> |
|---|
| 68 | | <section> |
|---|
| 69 | | <title>HTTPRedirect</title> |
|---|
| 70 | | <para>HTTPRedirect exceptions are not errors; therefore, there is no way to override their |
|---|
| 71 | | behavior. They set the response to an appropriate status, header set, and body, according to |
|---|
| 72 | | the HTTP spec.</para> |
|---|
| 73 | | </section> |
|---|
| | 76 | </example> |
|---|
| | 77 | |
|---|
| | 78 | <para>The default <code>_cpOnError</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.showTracebacks</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>_cpOnError</code>. Instead, they have their own |
|---|
| | 97 | <code>_cpOnHTTPError</code> function. Like <code>_cpOnError</code>, |
|---|
| | 98 | this is a "special attribute" and can be overridden by |
|---|
| | 99 | cherrypy.root objects. The default <code>_cpOnHTTPError</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 | |
|---|