From d7e2a77907cdc69a7edca948d33d04285f814562 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Wed, 26 Feb 2014 15:31:14 +0000 Subject: [PATCH] Refactor connection errors Makes command.py a lot more readable. --- fig/cli/command.py | 30 +++++++----------------------- fig/cli/errors.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/fig/cli/command.py b/fig/cli/command.py index 5ac0b48f..d4eb8d67 100644 --- a/fig/cli/command.py +++ b/fig/cli/command.py @@ -15,7 +15,7 @@ from ..service import ConfigError from .docopt_command import DocoptCommand from .formatter import Formatter from .utils import cached_property, docker_url, call_silently, is_mac, is_ubuntu -from .errors import UserError +from . import errors log = logging.getLogger(__name__) @@ -28,31 +28,15 @@ class Command(DocoptCommand): except ConnectionError: if call_silently(['which', 'docker']) != 0: if is_mac(): - raise UserError(""" -Couldn't connect to Docker daemon. You might need to install docker-osx: - -https://github.com/noplay/docker-osx -""") + raise errors.DockerNotFoundMac() elif is_ubuntu(): - raise UserError(""" -Couldn't connect to Docker daemon. You might need to install Docker: - -http://docs.docker.io/en/latest/installation/ubuntulinux/ -""") + raise errors.DockerNotFoundUbuntu() else: - raise UserError(""" -Couldn't connect to Docker daemon. You might need to install Docker: - -http://docs.docker.io/en/latest/installation/ -""") + raise errors.DockerNotFoundGeneric() elif call_silently(['which', 'docker-osx']) == 0: - raise UserError("Couldn't connect to Docker daemon - you might need to run `docker-osx shell`.") + raise errors.ConnectionErrorDockerOSX() else: - raise UserError(""" -Couldn't connect to Docker daemon at %s - is it running? - -If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable. -""" % self.client.base_url) + raise errors.ConnectionErrorGeneric(self.client.base_url) @cached_property def client(self): @@ -75,7 +59,7 @@ If it's at a non-standard location, specify the URL with the DOCKER_HOST environ try: return Project.from_config(self.project_name, config, self.client) except ConfigError as e: - raise UserError(six.text_type(e)) + raise errors.UserError(six.text_type(e)) @cached_property def project_name(self): diff --git a/fig/cli/errors.py b/fig/cli/errors.py index 874d3591..02ffe7b6 100644 --- a/fig/cli/errors.py +++ b/fig/cli/errors.py @@ -8,3 +8,46 @@ class UserError(Exception): def __unicode__(self): return self.msg + + +class DockerNotFoundMac(UserError): + def __init__(self): + super(DockerNotFoundMac, self).__init__(""" +Couldn't connect to Docker daemon. You might need to install docker-osx: + +https://github.com/noplay/docker-osx +""") + + +class DockerNotFoundUbuntu(UserError): + def __init__(self): + super(DockerNotFoundUbuntu, self).__init__(""" +Couldn't connect to Docker daemon. You might need to install Docker: + +http://docs.docker.io/en/latest/installation/ubuntulinux/ +""") + + +class DockerNotFoundGeneric(UserError): + def __init__(self): + super(DockerNotFoundGeneric, self).__init__(""" +Couldn't connect to Docker daemon. You might need to install Docker: + +http://docs.docker.io/en/latest/installation/ +""") + + +class ConnectionErrorDockerOSX(UserError): + def __init__(self): + super(ConnectionErrorDockerOSX, self).__init__(""" +Couldn't connect to Docker daemon - you might need to run `docker-osx shell`. +""") + + +class ConnectionErrorGeneric(UserError): + def __init__(self, url): + super(ConnectionErrorGeneric, self).__init__(""" +Couldn't connect to Docker daemon at %s - is it running? + +If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable. +""" % url)