diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index e8231d52..6aa8f9f1 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -75,7 +75,11 @@ class HTTPClient(object): def get_connection(self): _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): curl = ['curl -i -X %s' % method] @@ -126,10 +130,17 @@ class HTTPClient(object): kwargs['headers'].setdefault('X-Auth-Token', self.auth_token) self.log_curl_request(method, url, kwargs) - conn = self.get_connection() - conn.request(method, url, **kwargs) - resp = conn.getresponse() + + try: + conn.request(method, url, **kwargs) + except (socket.error, socket.gaierror): + raise exc.InvalidEndpoint() + + try: + resp = conn.getresponse() + except (socket.error, socket.timeout): + raise exc.CommunicationError() body_iter = ResponseBodyIterator(resp) diff --git a/glanceclient/exc.py b/glanceclient/exc.py index 41922a54..78ca4f73 100644 --- a/glanceclient/exc.py +++ b/glanceclient/exc.py @@ -16,14 +16,25 @@ 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""" - pass -class InvalidEndpoint(ValueError): - """The provided endpoint could not be used""" - pass +class InvalidEndpoint(BaseException): + """The provided endpoint is invalid.""" + + +class CommunicationError(BaseException): + """Unable to communicate with server.""" class ClientException(Exception):