From 99aa55736a5bf0caba1f6747576691e4a9c2abb4 Mon Sep 17 00:00:00 2001 From: Adam Beckmeyer Date: Sun, 23 Apr 2017 21:26:33 -0400 Subject: [PATCH 1/3] Add handling for exceptions raised by requests library Signed-off-by: Adam Beckmeyer --- matrix_client/api.py | 21 +++++++++++++-------- matrix_client/errors.py | 9 +++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index cb726aca..af09be77 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError try: from urllib import quote @@ -585,13 +585,18 @@ def _send(self, method, path, content=None, query_params={}, headers={}, response = None while True: - response = requests.request( - method, endpoint, - params=query_params, - data=content, - headers=headers, - verify=self.validate_cert - ) + try: + response = requests.request( + method, endpoint, + params=query_params, + data=content, + headers=headers, + verify=self.validate_cert + ) + except requests.exceptions.RequestException as e: + raise MatrixHttpLibError( + "Something went wrong in sending the request", e + ) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 10cd039f..f0d97ea2 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -5,6 +5,7 @@ class MatrixError(Exception): class MatrixUnexpectedResponse(MatrixError): """The home server gave an unexpected response. """ + def __init__(self, content=""): super(MatrixError, self).__init__(content) self.content = content @@ -17,3 +18,11 @@ def __init__(self, code=0, content=""): super(MatrixRequestError, self).__init__("%d: %s" % (code, content)) self.code = code self.content = content + + +class MatrixHttpLibError(MatrixError): + """The library used for http requests raised an exception.""" + + def __init__(self, msg, original_exception): + super(MatrixHttpLibError, self).__init__(msg + ": {}".format(original_exception)) + self.original_exception = original_exception From 10db9779d215654ce7ec161cbfdb71155366536d Mon Sep 17 00:00:00 2001 From: Adam Beckmeyer Date: Thu, 4 May 2017 14:54:13 -0400 Subject: [PATCH 2/3] Add more info when MatrixHttpLibError is raised Signed-off-by: Adam Beckmeyer --- matrix_client/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index af09be77..2c00eb7b 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -595,7 +595,7 @@ def _send(self, method, path, content=None, query_params={}, headers={}, ) except requests.exceptions.RequestException as e: raise MatrixHttpLibError( - "Something went wrong in sending the request", e + "Something went wrong in {} requesting {}".format(method, endpoint), e ) if response.status_code == 429: From 3982688b9496251a61176225d2cee35872bc3e2a Mon Sep 17 00:00:00 2001 From: Adam Beckmeyer Date: Tue, 24 Oct 2017 20:02:50 -0400 Subject: [PATCH 3/3] Construct error message inside of error instead of outside --- matrix_client/api.py | 4 +--- matrix_client/errors.py | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 2c00eb7b..44b9584a 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -594,9 +594,7 @@ def _send(self, method, path, content=None, query_params={}, headers={}, verify=self.validate_cert ) except requests.exceptions.RequestException as e: - raise MatrixHttpLibError( - "Something went wrong in {} requesting {}".format(method, endpoint), e - ) + raise MatrixHttpLibError(e, method, endpoint) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index f0d97ea2..1662dd3d 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -23,6 +23,8 @@ def __init__(self, code=0, content=""): class MatrixHttpLibError(MatrixError): """The library used for http requests raised an exception.""" - def __init__(self, msg, original_exception): - super(MatrixHttpLibError, self).__init__(msg + ": {}".format(original_exception)) + def __init__(self, original_exception, method, endpoint): + super(MatrixHttpLibError, self).__init__( + "Something went wrong in {} requesting {}: {}".format(original_exception) + ) self.original_exception = original_exception