* New pylast.MalformedResponseError exception that fires off (hopefully)
on bad responses from Last.fm or network data corruption. (Closes Issue #58)
This commit is contained in:
parent
5423322cd7
commit
c7d4227b01
21
pylast.py
21
pylast.py
|
@ -787,8 +787,11 @@ class _Request(object):
|
||||||
conn = HTTPConnection(host=HOST_NAME)
|
conn = HTTPConnection(host=HOST_NAME)
|
||||||
conn.request(method='POST', url=HOST_SUBDIR, body=data, headers=headers)
|
conn.request(method='POST', url=HOST_SUBDIR, body=data, headers=headers)
|
||||||
|
|
||||||
response = conn.getresponse()
|
try:
|
||||||
response_text = _unicode(response.read())
|
response_text = _unicode(conn.getresponse().read())
|
||||||
|
except Exception as e:
|
||||||
|
raise MalformedResponseError(self.network, e)
|
||||||
|
|
||||||
self._check_response_for_errors(response_text)
|
self._check_response_for_errors(response_text)
|
||||||
return response_text
|
return response_text
|
||||||
|
|
||||||
|
@ -805,7 +808,11 @@ class _Request(object):
|
||||||
def _check_response_for_errors(self, response):
|
def _check_response_for_errors(self, response):
|
||||||
"""Checks the response for errors and raises one if any exists."""
|
"""Checks the response for errors and raises one if any exists."""
|
||||||
|
|
||||||
|
try:
|
||||||
doc = minidom.parseString(_string(response))
|
doc = minidom.parseString(_string(response))
|
||||||
|
except Exception as e:
|
||||||
|
raise MalformedResponseError(self.network, e)
|
||||||
|
|
||||||
e = doc.getElementsByTagName('lfm')[0]
|
e = doc.getElementsByTagName('lfm')[0]
|
||||||
|
|
||||||
if e.getAttribute('status') != "ok":
|
if e.getAttribute('status') != "ok":
|
||||||
|
@ -1095,6 +1102,16 @@ class WSError(Exception):
|
||||||
|
|
||||||
return self.status
|
return self.status
|
||||||
|
|
||||||
|
class MalformedResponseError(Exception):
|
||||||
|
"""Exception conveying a malformed response from Last.fm."""
|
||||||
|
|
||||||
|
def __init__(self, network, underlying_error):
|
||||||
|
self.network = network
|
||||||
|
self.underlying_error = underlying_error
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Malformed response from Last.fm. Underlying error: %s" %str(self.underlying_error)
|
||||||
|
|
||||||
class Album(_BaseObject, _Taggable):
|
class Album(_BaseObject, _Taggable):
|
||||||
"""An album."""
|
"""An album."""
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue