X Tutup
Skip to content

Commit 5f1cc0f

Browse files
committed
add edge case for error handling re 6c41fac
1 parent 2be0b5e commit 5f1cc0f

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

tests/test_error_handling.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# encoding: utf-8
2+
from __future__ import unicode_literals, print_function
3+
4+
import json
5+
import re
6+
import sys
7+
import unittest
8+
import warnings
9+
10+
import twitter
11+
12+
warnings.filterwarnings('ignore', category=DeprecationWarning)
13+
14+
import responses
15+
from responses import GET, POST
16+
17+
DEFAULT_URL = re.compile(r'https?://.*\.twitter.com/1\.1/.*')
18+
BODY = b'{"request":"\\/1.1\\/statuses\\/user_timeline.json","error":"Not authorized."}'
19+
20+
21+
class ApiTest(unittest.TestCase):
22+
23+
def setUp(self):
24+
self.api = twitter.Api(
25+
consumer_key='test',
26+
consumer_secret='test',
27+
access_token_key='test',
28+
access_token_secret='test',
29+
sleep_on_rate_limit=False,
30+
chunk_size=500 * 1024)
31+
32+
@responses.activate
33+
def testGetShortUrlLength(self):
34+
responses.add(GET, DEFAULT_URL, body=BODY, status=401)
35+
36+
try:
37+
resp = self.api.GetUserTimeline(screen_name="twitter")
38+
except twitter.TwitterError as e:
39+
self.assertEqual(e.message, "Not authorized.")

twitter/api.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ def UploadMediaSimple(self,
10641064
url = '%s/media/upload.json' % self.upload_url
10651065
parameters = {}
10661066

1067-
media_fp, filename, file_size, media_type = parse_media_file(media)
1067+
media_fp, _, _, _ = parse_media_file(media)
10681068

10691069
parameters['media'] = media_fp.read()
10701070

@@ -3263,7 +3263,6 @@ def IncomingFriendship(self,
32633263
parameters['count'] = int(cursor)
32643264
except ValueError:
32653265
raise TwitterError({'message': "cursor must be an integer"})
3266-
break
32673266
resp = self._RequestUrl(url, 'GET', data=parameters)
32683267
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
32693268
result += [x for x in data['ids']]
@@ -3308,7 +3307,6 @@ def OutgoingFriendship(self,
33083307
parameters['count'] = int(cursor)
33093308
except ValueError:
33103309
raise TwitterError({'message': "cursor must be an integer"})
3311-
break
33123310
resp = self._RequestUrl(url, 'GET', data=parameters)
33133311
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
33143312
result += [x for x in data['ids']]
@@ -4786,27 +4784,19 @@ def _ParseAndCheckTwitter(self, json_data):
47864784
This is a purely defensive check because during some Twitter
47874785
network outages it will return an HTML failwhale page.
47884786
"""
4789-
data = None
47904787
try:
47914788
data = json.loads(json_data)
4792-
try:
4793-
self._CheckForTwitterError(data)
4794-
4795-
except ValueError:
4796-
if "<title>Twitter / Over capacity</title>" in json_data:
4797-
raise TwitterError({'message': "Capacity Error"})
4798-
if "<title>Twitter / Error</title>" in json_data:
4799-
raise TwitterError({'message': "Technical Error"})
4800-
if "Exceeded connection limit for user" in json_data:
4801-
raise TwitterError({'message': "Exceeded connection limit for user"})
4802-
if "Error 401 Unauthorized" in json_data:
4803-
raise TwitterError({'message': "Unauthorized"})
4804-
raise TwitterError({'message': "Unknown error, try addeding "})
4805-
4806-
except:
4789+
except ValueError:
4790+
if "<title>Twitter / Over capacity</title>" in json_data:
4791+
raise TwitterError({'message': "Capacity Error"})
4792+
if "<title>Twitter / Error</title>" in json_data:
4793+
raise TwitterError({'message': "Technical Error"})
4794+
if "Exceeded connection limit for user" in json_data:
4795+
raise TwitterError({'message': "Exceeded connection limit for user"})
48074796
if "Error 401 Unauthorized" in json_data:
48084797
raise TwitterError({'message': "Unauthorized"})
4809-
4798+
raise TwitterError({'Unknown error: {0}'.format(json_data)})
4799+
self._CheckForTwitterError(data)
48104800
return data
48114801

48124802
def _CheckForTwitterError(self, data):

0 commit comments

Comments
 (0)
X Tutup