Handle communication failures cleanly
Expand exceptions to cover more failures cases. This adds CommunicationFailure to represent any failures while attempting to communicate with the remote endpoint. This also adds a new base exception class BaseException which should be used for all non-HTTP related failures. Change-Id: Ie3e1d45c520d816a3f491a85fde94a6c4edf295e
This commit is contained in:
@@ -75,7 +75,11 @@ class HTTPClient(object):
|
|||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
_class = self.connection_params[0]
|
_class = self.connection_params[0]
|
||||||
return _class(*self.connection_params[1], **self.connection_params[2])
|
try:
|
||||||
|
return _class(*self.connection_params[1],
|
||||||
|
**self.connection_params[2])
|
||||||
|
except httplib.InvalidUrl:
|
||||||
|
raise exc.InvalidEndpoint()
|
||||||
|
|
||||||
def log_curl_request(self, method, url, kwargs):
|
def log_curl_request(self, method, url, kwargs):
|
||||||
curl = ['curl -i -X %s' % method]
|
curl = ['curl -i -X %s' % method]
|
||||||
@@ -126,10 +130,17 @@ class HTTPClient(object):
|
|||||||
kwargs['headers'].setdefault('X-Auth-Token', self.auth_token)
|
kwargs['headers'].setdefault('X-Auth-Token', self.auth_token)
|
||||||
|
|
||||||
self.log_curl_request(method, url, kwargs)
|
self.log_curl_request(method, url, kwargs)
|
||||||
|
|
||||||
conn = self.get_connection()
|
conn = self.get_connection()
|
||||||
|
|
||||||
|
try:
|
||||||
conn.request(method, url, **kwargs)
|
conn.request(method, url, **kwargs)
|
||||||
|
except (socket.error, socket.gaierror):
|
||||||
|
raise exc.InvalidEndpoint()
|
||||||
|
|
||||||
|
try:
|
||||||
resp = conn.getresponse()
|
resp = conn.getresponse()
|
||||||
|
except (socket.error, socket.timeout):
|
||||||
|
raise exc.CommunicationError()
|
||||||
|
|
||||||
body_iter = ResponseBodyIterator(resp)
|
body_iter = ResponseBodyIterator(resp)
|
||||||
|
|
||||||
|
@@ -16,14 +16,25 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class CommandError(Exception):
|
class BaseException(Exception):
|
||||||
|
"""An error occurred."""
|
||||||
|
def __init__(self, message=None):
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.message or self.__class__.__doc__
|
||||||
|
|
||||||
|
|
||||||
|
class CommandError(BaseException):
|
||||||
"""Invalid usage of CLI"""
|
"""Invalid usage of CLI"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidEndpoint(ValueError):
|
class InvalidEndpoint(BaseException):
|
||||||
"""The provided endpoint could not be used"""
|
"""The provided endpoint is invalid."""
|
||||||
pass
|
|
||||||
|
|
||||||
|
class CommunicationError(BaseException):
|
||||||
|
"""Unable to communicate with server."""
|
||||||
|
|
||||||
|
|
||||||
class ClientException(Exception):
|
class ClientException(Exception):
|
||||||
|
Reference in New Issue
Block a user