Commit b5eab640 authored by Vincent Pelletier's avatar Vincent Pelletier

client: Catch SSL errors.

parent 3751896f
......@@ -36,6 +36,8 @@ from . import version
__all__ = (
'CaucaseError',
'CaucaseHTTPError',
'CaucaseSSLError',
'CaucaseClient',
'HTTPSOnlyCaucaseClient',
)
......@@ -43,11 +45,23 @@ __all__ = (
_cryptography_backend = default_backend()
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.
"""
pass
class CaucaseSSLError(CaucaseError):
"""
Raised when there is an SSL error while communicating with the server.
"""
pass
class CaucaseClient(object):
"""
Caucase HTTP(S) client.
......@@ -171,11 +185,18 @@ class CaucaseClient(object):
path = self._path + url
headers = headers or {}
headers.setdefault('User-Agent', 'caucase ' + version.__version__)
connection.request(method, path, body, headers)
response = connection.getresponse()
response_body = response.read()
try:
connection.request(method, path, body, headers)
response = connection.getresponse()
response_body = response.read()
except ssl.SSLError as exc:
raise CaucaseSSLError(exc.errno, exc.strerror)
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
if response.status == 201:
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