Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improve handling of invalid responses from TMDB

When an HTML error is received from the TMDB API, this will now attempt
to process a specific error type from the returned JSON.  If no error
type is given, or the resposnse was not JSON, it will raise the previous
TMDBHTTPError.
  • Loading branch information...
commit 7425634d7b92f8bbe9abdc8b50c4148c96c87f40 1 parent c2ef5d1
@wagnerrp authored
Showing with 30 additions and 8 deletions.
  1. +21 −6 tmdb3/request.py
  2. +9 −2 tmdb3/tmdb_exceptions.py
View
27 tmdb3/request.py
@@ -94,7 +94,7 @@ def open(self):
print ' '+self.get_data()
return urllib2.urlopen(self)
except urllib2.HTTPError, e:
- raise TMDBHTTPError(str(e))
+ raise TMDBHTTPError(e)
def read(self):
"""Return result from specified URL as a string."""
@@ -104,7 +104,21 @@ def read(self):
def readJSON(self):
"""Parse result from specified URL as JSON data."""
url = self.get_full_url()
- data = json.load(self.open())
+ try:
+ # catch HTTP error from open()
+ data = json.load(self.open())
+ except TMDBHTTPError, e:
+ try:
+ # try to load whatever was returned
+ data = json.loads(e.response)
+ except:
+ # cannot parse json, just raise existing error
+ raise e
+ else:
+ # response parsed, try to raise error from TMDB
+ handle_status(data, url)
+ # no error from TMDB, just raise existing error
+ raise e
handle_status(data, url)
if DEBUG:
import pprint
@@ -113,14 +127,14 @@ def readJSON(self):
status_handlers = {
1: None,
- 2: TMDBRequestError('Invalid service - This service does not exist.'),
+ 2: TMDBRequestInvalid('Invalid service - This service does not exist.'),
3: TMDBRequestError('Authentication Failed - You do not have '+\
'permissions to access this service.'),
- 4: TMDBRequestError("Invalid format - This service doesn't exist "+\
+ 4: TMDBRequestInvalid("Invalid format - This service doesn't exist "+\
'in that format.'),
- 5: TMDBRequestError('Invalid parameters - Your request parameters '+\
+ 5: TMDBRequestInvalid('Invalid parameters - Your request parameters '+\
'are incorrect.'),
- 6: TMDBRequestError('Invalid id - The pre-requisite id is invalid '+\
+ 6: TMDBRequestInvalid('Invalid id - The pre-requisite id is invalid '+\
'or not found.'),
7: TMDBKeyInvalid('Invalid API key - You must be granted a valid key.'),
8: TMDBRequestError('Duplicate entry - The data you tried to submit '+\
@@ -139,5 +153,6 @@ def readJSON(self):
def handle_status(data, query):
status = status_handlers[data.get('status_code', 1)]
if status is not None:
+ status.tmdberrno = data['status_code']
status.query = query
raise status
View
11 tmdb3/tmdb_exceptions.py
@@ -13,6 +13,7 @@ class TMDBError( Exception ):
KeyInvalid = 30
KeyRevoked = 40
RequestError = 50
+ RequestInvalid = 51
PagingIssue = 60
CacheError = 70
CacheReadError = 71
@@ -25,7 +26,7 @@ class TMDBError( Exception ):
def __init__(self, msg=None, errno=0):
self.errno = errno
- if errno:
+ if errno == 0:
self.errno = getattr(self, 'TMDB'+self.__class__.__name__, errno)
self.args = (msg,)
@@ -44,6 +45,9 @@ class TMDBKeyRevoked( TMDBKeyInvalid ):
class TMDBRequestError( TMDBError ):
pass
+class TMDBRequestInvalid( TMDBRequestError ):
+ pass
+
class TMDBPagingIssue( TMDBRequestError ):
pass
@@ -72,7 +76,10 @@ class TMDBImageSizeError( TMDBError ):
pass
class TMDBHTTPError( TMDBError ):
- pass
+ def __init__(self, err):
+ self.httperrno = err.code
+ self.response = err.fp.read()
+ super(TMDBHTTPError, self).__init__(str(err))
class TMDBOffline( TMDBError ):
pass
Please sign in to comment.
Something went wrong with that request. Please try again.