Refactored connection error handling

This commit is contained in:
Honza Král
2016-12-15 16:46:05 +11:00
parent c2fc5aa18c
commit b9ba12977f
2 changed files with 10 additions and 13 deletions
+5 -7
View File
@@ -74,14 +74,12 @@ class RequestsHttpConnection(Connection):
timeout=timeout or self.timeout)
duration = time.time() - start
raw_data = response.text
except requests.exceptions.SSLError as e:
self.log_request_fail(method, url, prepared_request.path_url, body, time.time() - start, exception=e)
raise SSLError('N/A', str(e), e)
except requests.Timeout as e:
self.log_request_fail(method, url, prepared_request.path_url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except requests.ConnectionError as e:
except Exception as e:
self.log_request_fail(method, url, prepared_request.path_url, body, time.time() - start, exception=e)
if isinstance(e, requests.exceptions.SSLError):
raise SSLError('N/A', str(e), e)
if isinstance(e, requests.Timeout):
raise ConnectionTimeout('TIMEOUT', str(e), e)
raise ConnectionError('N/A', str(e), e)
# raise errors based on http status codes, let the client handle those if needed
+5 -6
View File
@@ -109,16 +109,15 @@ class Urllib3HttpConnection(Connection):
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except UrllibSSLError as e:
self.log_request_fail(method, full_url, url, body, time.time() - start, exception=e)
raise SSLError('N/A', str(e), e)
except ReadTimeoutError as e:
self.log_request_fail(method, full_url, url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except Exception as e:
self.log_request_fail(method, full_url, url, body, time.time() - start, exception=e)
if isinstance(e, UrllibSSLError):
raise SSLError('N/A', str(e), e)
if isinstance(e, ReadTimeoutError):
raise ConnectionTimeout('TIMEOUT', str(e), e)
raise ConnectionError('N/A', str(e), e)
# raise errors based on http status codes, let the client handle those if needed
if not (200 <= response.status < 300) and response.status not in ignore:
self.log_request_fail(method, full_url, url, body, duration, response.status, raw_data)
self._raise_error(response.status, raw_data)