Imported Upstream version 12.1.0
[contrib/python-twisted.git] / doc / web / howto / web-in-60 / logging-errors.html
1 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
2   <head>
3 <title>Twisted Documentation: Logging Errors</title>
4 <link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
8     <h1 class="title">Logging Errors</h1>
9     <div class="toc"><ol/></div>
10     <div class="content">
11 <span/>
12
13 <p>The <a href="interrupted.html" shape="rect">previous example</a> created a server that
14 dealt with response errors by aborting response generation, potentially avoiding
15 pointless work. However, it did this silently for any error. In this example,
16 we'll modify the previous example so that it logs each failed response.</p>
17
18 <p>This example will use the Twisted API for logging errors. As was
19 mentioned in the <a href="asynchronous-deferred.html" shape="rect">first example
20 covering Deferreds</a>, errbacks are passed an error. In the previous
21 example, the <code>_responseFailed</code> errback accepted this error
22 as a parameter but ignored it. The only way this example will differ
23 is that this <code>_responseFailed</code> will use that error
24 parameter to log a message.</p>
25
26 <p>This example will require all of the imports required by the previous example
27 plus one new import:</p>
28
29 <pre class="python"><p class="py-linenumber">1
30 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">log</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">err</span>
31 </pre>
32
33 <p>The only other part of the previous example which changes is
34 the <code>_responseFailed</code> callback, which will now log the
35 error passed to it:</p>
36
37 <pre class="python"><p class="py-linenumber">1
38 2
39 3
40 4
41 </p>...
42     <span class="py-src-keyword">def</span> <span class="py-src-identifier">_responseFailed</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">failure</span>, <span class="py-src-parameter">call</span>):
43         <span class="py-src-variable">call</span>.<span class="py-src-variable">cancel</span>()
44         <span class="py-src-variable">err</span>(<span class="py-src-variable">failure</span>, <span class="py-src-string">&quot;Async response demo interrupted response&quot;</span>)
45 </pre>
46
47 <p>We're passing two arguments to <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.python.log.err.html" title="twisted.python.log.err">err</a></code> here. The first is the error which is being
48 passed in to the callback. This is always an object of type <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.python.failure.Failure.html" title="twisted.python.failure.Failure">Failure</a></code>, a class which represents an
49 exception and (sometimes, but not always) a traceback. <code>err</code> will
50 format this nicely for the log. The second argument is a descriptive string that
51 tells someone reading the log what the source of the error was.</p>
52
53 <p>Here's the full example with the two above modifications:</p>
54
55 <pre class="python"><p class="py-linenumber"> 1
56  2
57  3
58  4
59  5
60  6
61  7
62  8
63  9
64 10
65 11
66 12
67 13
68 14
69 15
70 16
71 17
72 18
73 19
74 20
75 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
76 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">NOT_DONE_YET</span>
77 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
78 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">log</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">err</span>
79
80 <span class="py-src-keyword">class</span> <span class="py-src-identifier">DelayedResource</span>(<span class="py-src-parameter">Resource</span>):
81     <span class="py-src-keyword">def</span> <span class="py-src-identifier">_delayedRender</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
82         <span class="py-src-variable">request</span>.<span class="py-src-variable">write</span>(<span class="py-src-string">&quot;&lt;html&gt;&lt;body&gt;Sorry to keep you waiting.&lt;/body&gt;&lt;/html&gt;&quot;</span>)
83         <span class="py-src-variable">request</span>.<span class="py-src-variable">finish</span>()
84
85     <span class="py-src-keyword">def</span> <span class="py-src-identifier">_responseFailed</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">failure</span>, <span class="py-src-parameter">call</span>):
86         <span class="py-src-variable">call</span>.<span class="py-src-variable">cancel</span>()
87         <span class="py-src-variable">err</span>(<span class="py-src-variable">failure</span>, <span class="py-src-string">&quot;Async response demo interrupted response&quot;</span>)
88
89     <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
90         <span class="py-src-variable">call</span> = <span class="py-src-variable">reactor</span>.<span class="py-src-variable">callLater</span>(<span class="py-src-number">5</span>, <span class="py-src-variable">self</span>.<span class="py-src-variable">_delayedRender</span>, <span class="py-src-variable">request</span>)
91         <span class="py-src-variable">request</span>.<span class="py-src-variable">notifyFinish</span>().<span class="py-src-variable">addErrback</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">_responseFailed</span>, <span class="py-src-variable">call</span>)
92         <span class="py-src-keyword">return</span> <span class="py-src-variable">NOT_DONE_YET</span>
93
94 <span class="py-src-variable">resource</span> = <span class="py-src-variable">DelayedResource</span>()
95 </pre>
96
97 <p>Run this server as in the <a href="interrupted.html" shape="rect">previous example</a>
98 and interrupt a request. Unlike the previous example, where the server gave no
99 indication that this had happened, you'll see a message in the log output with
100 this version.</p>
101
102 </div>
103
104     <p><a href="../index.html">Index</a></p>
105     <span class="version">Version: 12.1.0</span>
106   </body>
107 </html>