Refacor handling storage exceptions stemming from unique names
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/854cfbb9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/854cfbb9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/854cfbb9 Branch: refs/heads/logger_task Commit: 854cfbb98904617691b84039d8fbf35002d8c7a2 Parents: e92ad21 Author: Avia Efrat <[email protected]> Authored: Mon Apr 3 15:44:00 2017 +0300 Committer: Ran Ziv <[email protected]> Committed: Thu Apr 6 11:29:17 2017 +0300 ---------------------------------------------------------------------- aria/cli/commands/service_templates.py | 13 ++++--------- aria/cli/commands/services.py | 11 +++-------- aria/cli/constants.py | 4 ---- aria/cli/utils.py | 9 +++++++++ 4 files changed, 16 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/854cfbb9/aria/cli/commands/service_templates.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands/service_templates.py b/aria/cli/commands/service_templates.py index 79f8012..6c6224b 100644 --- a/aria/cli/commands/service_templates.py +++ b/aria/cli/commands/service_templates.py @@ -21,9 +21,9 @@ from .. import utils from .. import csar from .. import service_template_utils from ..cli import aria -from ..constants import TWO_MODELS_WITH_THE_SAME_NAME_ERROR_TEMPLATE from ..table import print_data from ..exceptions import AriaCliError +from ..utils import handle_storage_exception from ...core import Core from ...exceptions import AriaException from ...storage import exceptions as storage_exceptions @@ -119,14 +119,9 @@ def store(service_template_path, service_template_name, model_storage, resource_ core.create_service_template(service_template_path, os.path.dirname(service_template_path), service_template_name) - except storage_exceptions.StorageError: - logger.info(TWO_MODELS_WITH_THE_SAME_NAME_ERROR_TEMPLATE.format( - model_class='service template', - name=service_template_name)) - raise - - else: - logger.info('Service template stored') + except storage_exceptions.StorageError as e: + handle_storage_exception(e, 'service template', service_template_name) + logger.info('Service template stored') @service_templates.command(name='delete', http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/854cfbb9/aria/cli/commands/services.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands/services.py b/aria/cli/commands/services.py index 28fb499..e73c43b 100644 --- a/aria/cli/commands/services.py +++ b/aria/cli/commands/services.py @@ -19,10 +19,9 @@ from StringIO import StringIO from . import service_templates from ..cli import aria, helptexts -from ..constants import TWO_MODELS_WITH_THE_SAME_NAME_ERROR_TEMPLATE from ..exceptions import AriaCliError from ..table import print_data -from ..utils import storage_sort_param +from ..utils import storage_sort_param, handle_storage_exception from ...core import Core from ...exceptions import AriaException from ...storage import exceptions as storage_exceptions @@ -99,16 +98,12 @@ def create(service_template_name, try: core = Core(model_storage, resource_storage, plugin_manager) service = core.create_service(service_template_name, inputs, service_name) - except storage_exceptions.StorageError: - logger.info(TWO_MODELS_WITH_THE_SAME_NAME_ERROR_TEMPLATE.format( - model_class='service', - name=service_name)) - raise + except storage_exceptions.StorageError as e: + handle_storage_exception(e, 'service', service_name) except AriaException as e: logger.info(str(e)) service_templates.print_service_template_inputs(model_storage, service_template_name) raise AriaCliError(str(e)) - logger.info("Service created. The service's name is {0}".format(service.name)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/854cfbb9/aria/cli/constants.py ---------------------------------------------------------------------- diff --git a/aria/cli/constants.py b/aria/cli/constants.py index fdd37fc..67c094d 100644 --- a/aria/cli/constants.py +++ b/aria/cli/constants.py @@ -16,7 +16,3 @@ SAMPLE_SERVICE_TEMPLATE_FILENAME = 'service_template.yaml' HELP_TEXT_COLUMN_BUFFER = 5 -TWO_MODELS_WITH_THE_SAME_NAME_ERROR_TEMPLATE = """ -Could not store {model_class} `{name}` -There already a exists a {model_class} with the same name -""" http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/854cfbb9/aria/cli/utils.py ---------------------------------------------------------------------- diff --git a/aria/cli/utils.py b/aria/cli/utils.py index 3b68729..fff8e3a 100644 --- a/aria/cli/utils.py +++ b/aria/cli/utils.py @@ -150,3 +150,12 @@ def generate_progress_handler(file_path, action='', max_bar_length=80): sys.stdout.write('\n') return print_progress + + +def handle_storage_exception(e, model_class, name): + if 'UNIQUE constraint failed' in e.msg: + msg = 'Could not store {model_class} `{name}`\n' \ + 'There already a exists a {model_class} with the same name' \ + .format(model_class=model_class, name=name) + raise AriaCliError(msg) + raise
