-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.html
More file actions
103 lines (89 loc) · 7.57 KB
/
Copy patherrors.html
File metadata and controls
103 lines (89 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Error Handling — TwitterAPI 2.3 documentation</title>
<link rel="stylesheet" href="_static/haiku.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/print.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/theme_extras.js"></script>
<link rel="top" title="TwitterAPI 2.3 documentation" href="index.html" />
<link rel="next" title="Paging Results" href="paging.html" />
<link rel="prev" title="Authentication" href="authentication.html" />
</head>
<body>
<div class="header"><h1 class="heading"><a href="index.html">
<span>TwitterAPI 2.3 documentation</span></a></h1>
<h2 class="heading"><span>Error Handling</span></h2>
</div>
<div class="topnav">
<p>
«  <a href="authentication.html">Authentication</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="paging.html">Paging Results</a>  »
</p>
</div>
<div class="content">
<div class="section" id="error-handling">
<h1>Error Handling<a class="headerlink" href="#error-handling" title="Permalink to this headline">¶</a></h1>
<p>Besides tweet statuses, the REST API and Streaming API iterators may return error and other messages. It is up to the application to test what type of object has been returned. Message types are documented <a class="reference external" href="http://dev.twitter.com/overview/api/response-codes">here</a> and <a class="reference external" href="http://dev.twitter.com/streaming/overview/messages-types">here</a>.</p>
<div class="section" id="rest-api-messages">
<h2>REST API Messages<a class="headerlink" href="#rest-api-messages" title="Permalink to this headline">¶</a></h2>
<p>REST API endpoints can return many more types of messages than Streaming API endpoints. Depending on the endpoint, you may want to handle a particular type of message, such as exceeding a rate limit or posting a duplicate tweet. Here is a general pattern for simply printing out any message and error code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">'search/tweets'</span><span class="p">,</span> <span class="p">{</span><span class="s">'q'</span><span class="p">:</span><span class="s">'pizza'</span><span class="p">})</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">get_iterator</span><span class="p">():</span>
<span class="k">if</span> <span class="s">'text'</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span>
<span class="k">print</span> <span class="n">item</span><span class="p">[</span><span class="s">'text'</span><span class="p">]</span>
<span class="k">elif</span> <span class="s">'message'</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'</span><span class="si">%s</span><span class="s"> (</span><span class="si">%d</span><span class="s">)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">item</span><span class="p">[</span><span class="s">'message'</span><span class="p">],</span> <span class="n">item</span><span class="p">[</span><span class="s">'code'</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="streaming-api-messages">
<h2>Streaming API Messages<a class="headerlink" href="#streaming-api-messages" title="Permalink to this headline">¶</a></h2>
<p>Streaming API endpoints return a variety of messages, most are not really errors. For example, a “limit” message contains the number of tweets missing from the stream. This happens when the number of tweets matching your filter exceeds a threshold set by Twitter. Other useful messages are “disconnect” and “delete”. The pattern is similar to the one preceding:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">'statuses/filter'</span><span class="p">,</span> <span class="p">{</span><span class="s">'track'</span><span class="p">:</span><span class="s">'pizza'</span><span class="p">})</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">get_iterator</span><span class="p">():</span>
<span class="k">if</span> <span class="s">'text'</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span>
<span class="k">print</span> <span class="n">item</span><span class="p">[</span><span class="s">'text'</span><span class="p">]</span>
<span class="k">elif</span> <span class="s">'limit'</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'</span><span class="si">%d</span><span class="s"> tweets missed'</span> <span class="o">%</span> <span class="n">item</span><span class="p">[</span><span class="s">'limit'</span><span class="p">][</span><span class="s">'track'</span><span class="p">]</span>
<span class="k">elif</span> <span class="s">'disconnect'</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'disconnecting because </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">item</span><span class="p">[</span><span class="s">'disconnect'</span><span class="p">][</span><span class="s">'reason'</span><span class="p">]</span>
<span class="k">break</span>
</pre></div>
</div>
<p>Even if you are not interested in handling errors it is necessary to test that the object returned by an iterator is a valid tweet status before using the object.</p>
</div>
</div>
</div>
<div class="bottomnav">
<p>
«  <a href="authentication.html">Authentication</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="paging.html">Paging Results</a>  »
</p>
</div>
<div class="footer">
© Copyright 2014, geduldig.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b2.
</div>
</body>
</html>