From f7b89bfafae34fa22509c1d1c59d1284ec62c5df Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 23 Apr 2014 21:35:06 +0530 Subject: exception handling changes --- python-tornado/unassembled/Platform.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'python-tornado/unassembled/Platform.py') diff --git a/python-tornado/unassembled/Platform.py b/python-tornado/unassembled/Platform.py index 871a400..b0e0be9 100644 --- a/python-tornado/unassembled/Platform.py +++ b/python-tornado/unassembled/Platform.py @@ -51,7 +51,8 @@ class Pubnub(PubnubCoreAsync): self.http = tornado.httpclient.AsyncHTTPClient(max_clients=1000) self.id = None - def _request(self, request, callback=None, error=None, single=False): + def _request(self, request, callback=None, error=None, + single=False, read_timeout=5, connect_timeout=5): def _invoke(func, data): if func is not None: @@ -61,8 +62,8 @@ class Pubnub(PubnubCoreAsync): request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers, - connect_timeout=10, - request_timeout=310) + connect_timeout=connect_timeout, + request_timeout=read_timeout) if single is True: id = time.time() self.id = id @@ -76,20 +77,23 @@ class Pubnub(PubnubCoreAsync): if body is None: return - #print(body) def handle_exc(*args): return True if response.error is not None: with ExceptionStackContext(handle_exc): - response.rethrow() + if response.code in [403, 401]: + response.rethrow() + else: + _invoke(error, {"message": response.reason}) return + try: data = json.loads(body) except TypeError as e: try: data = json.loads(body.decode("utf-8")) - except: + except ValueError as ve: _invoke(error, {'error': 'json decode error'}) if 'error' in data and 'status' in data and 'status' != 200: -- cgit v1.2.3