From c7d4227b015c31bd7ea20897044b994d5dae6ae2 Mon Sep 17 00:00:00 2001 From: Amr Hassan Date: Fri, 21 Jan 2011 21:49:07 +0000 Subject: [PATCH] * New pylast.MalformedResponseError exception that fires off (hopefully) on bad responses from Last.fm or network data corruption. (Closes Issue #58) --- .build | 2 +- pylast.py | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.build b/.build index 7813681..62f9457 100644 --- a/.build +++ b/.build @@ -1 +1 @@ -5 \ No newline at end of file +6 \ No newline at end of file diff --git a/pylast.py b/pylast.py index 4d84560..cebf1e0 100644 --- a/pylast.py +++ b/pylast.py @@ -787,8 +787,11 @@ class _Request(object): conn = HTTPConnection(host=HOST_NAME) conn.request(method='POST', url=HOST_SUBDIR, body=data, headers=headers) - response = conn.getresponse() - response_text = _unicode(response.read()) + try: + response_text = _unicode(conn.getresponse().read()) + except Exception as e: + raise MalformedResponseError(self.network, e) + self._check_response_for_errors(response_text) return response_text @@ -805,7 +808,11 @@ class _Request(object): def _check_response_for_errors(self, response): """Checks the response for errors and raises one if any exists.""" - doc = minidom.parseString(_string(response)) + try: + doc = minidom.parseString(_string(response)) + except Exception as e: + raise MalformedResponseError(self.network, e) + e = doc.getElementsByTagName('lfm')[0] if e.getAttribute('status') != "ok": @@ -1095,6 +1102,16 @@ class WSError(Exception): 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): """An album."""