Skip to content

Commit 4bbe95e

Browse files
committed
Catch invalid errors in urllib2 from 2.4 and 2.5
urllib2 in Python 2.4 and 2.5 throws error for all non 200 responses. Added support to catch these for 201 and 204 response codes.
1 parent 71ca0da commit 4bbe95e

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

ox3apiclient/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,23 @@ def request(self, url, method='GET', headers={}, data=None, sign=False):
140140
if data:
141141
req.add_data(urllib.urlencode(req.get_data()))
142142

143-
return urllib2.urlopen(req)
143+
# In 2.4 and 2.5, urllib2 throws errors for all non 200 status codes.
144+
# The OpenX API uses 201 create responses and 204 for delete respones.
145+
# We'll catch those errors and return the HTTPError object since it can
146+
# (thankfully) be used just like a Response object. A handler is
147+
# probably a better approach, but this is quick and works.
148+
res = '[]'
149+
try:
150+
res = urllib2.urlopen(req)
151+
except urllib2.HTTPError, err:
152+
if err.code in [201, 204]:
153+
res = err
154+
else:
155+
# TODO: Decide on format and what extra data to alert user for
156+
# troubleshooting.
157+
raise err
158+
159+
return res
144160

145161
def fetch_request_token(self):
146162
"""Helper method to fetch and set request token.

0 commit comments

Comments
 (0)