Commit b5eab640 authored by Vincent Pelletier's avatar Vincent Pelletier

client: Catch SSL errors.

parent 3751896f
...@@ -36,6 +36,8 @@ from . import version ...@@ -36,6 +36,8 @@ from . import version
__all__ = ( __all__ = (
'CaucaseError', 'CaucaseError',
'CaucaseHTTPError',
'CaucaseSSLError',
'CaucaseClient', 'CaucaseClient',
'HTTPSOnlyCaucaseClient', 'HTTPSOnlyCaucaseClient',
) )
...@@ -43,11 +45,23 @@ __all__ = ( ...@@ -43,11 +45,23 @@ __all__ = (
_cryptography_backend = default_backend() _cryptography_backend = default_backend()
class CaucaseError(Exception): class CaucaseError(Exception):
"""
Base error for errors when communicating with a caucase server.
"""
pass
class CaucaseHTTPError(CaucaseError):
""" """
Raised when server responds with an HTTP error status. Raised when server responds with an HTTP error status.
""" """
pass pass
class CaucaseSSLError(CaucaseError):
"""
Raised when there is an SSL error while communicating with the server.
"""
pass
class CaucaseClient(object): class CaucaseClient(object):
""" """
Caucase HTTP(S) client. Caucase HTTP(S) client.
...@@ -171,11 +185,18 @@ class CaucaseClient(object): ...@@ -171,11 +185,18 @@ class CaucaseClient(object):
path = self._path + url path = self._path + url
headers = headers or {} headers = headers or {}
headers.setdefault('User-Agent', 'caucase ' + version.__version__) headers.setdefault('User-Agent', 'caucase ' + version.__version__)
try:
connection.request(method, path, body, headers) connection.request(method, path, body, headers)
response = connection.getresponse() response = connection.getresponse()
response_body = response.read() response_body = response.read()
except ssl.SSLError as exc:
raise CaucaseSSLError(exc.errno, exc.strerror)
if response.status >= 400: if response.status >= 400:
raise CaucaseError(response.status, response.getheaders(), response_body) raise CaucaseHTTPError(
response.status,
response.getheaders(),
response_body,
)
assert response.status < 300 # caucase does not redirect assert response.status < 300 # caucase does not redirect
if response.status == 201: if response.status == 201:
return response.getheader('Location') return response.getheader('Location')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment