diff --git a/matrix_client/api.py b/matrix_client/api.py index cb726aca..44b9584a 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,16 @@ 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(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 10cd039f..1662dd3d 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,13 @@ 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, original_exception, method, endpoint): + super(MatrixHttpLibError, self).__init__( + "Something went wrong in {} requesting {}: {}".format(original_exception) + ) + self.original_exception = original_exception