From b267c90b8273fe3282cf081b7f46d55b90f93782 Mon Sep 17 00:00:00 2001 From: Amr Hassan Date: Fri, 21 Jan 2011 22:33:34 +0000 Subject: [PATCH] * Further Error-proofing through pylast.NetworkError. --- pylast.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pylast.py b/pylast.py index d208bb7..46ec921 100644 --- a/pylast.py +++ b/pylast.py @@ -781,11 +781,20 @@ class _Request(object): if self.network.is_proxy_enabled(): conn = HTTPConnection(host = self._get_proxy()[0], port = self._get_proxy()[1]) - conn.request(method='POST', url="http://" + HOST_NAME + HOST_SUBDIR, - body=data, headers=headers) + + try: + conn.request(method='POST', url="http://" + HOST_NAME + HOST_SUBDIR, + body=data, headers=headers) + except Exception as e: + raise NetworkError(self.network, e) + else: conn = HTTPConnection(host=HOST_NAME) - conn.request(method='POST', url=HOST_SUBDIR, body=data, headers=headers) + + try: + conn.request(method='POST', url=HOST_SUBDIR, body=data, headers=headers) + except Exception as e: + raise NetworkError(self.network, e) try: response_text = _unicode(conn.getresponse().read()) @@ -1112,6 +1121,16 @@ class MalformedResponseError(Exception): def __str__(self): return "Malformed response from Last.fm. Underlying error: %s" %str(self.underlying_error) +class NetworkError(Exception): + """Exception conveying a problem in sending a request to Last.fm""" + + def __init__(self, network, underlying_error): + self.network = network + self.underlying_error = underlying_error + + def __str__(self): + return "NetworkError: %s" %str(self.underlying_error) + class Album(_BaseObject, _Taggable): """An album."""