From 3c693964b31b3e87e425af0a69ac7f5f3730859e Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 28 Jun 2022 18:11:47 +0000 Subject: [PATCH] adjust error handling --- config/main.py | 21 ++++++++++----------- config/validated_config_db_connector.py | 10 ++-------- tests/portchannel_test.py | 10 +++++----- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/config/main.py b/config/main.py index 3d0919e415..8662f39ebe 100644 --- a/config/main.py +++ b/config/main.py @@ -1895,10 +1895,7 @@ def portchannel(db, ctx, namespace): def add_portchannel(ctx, portchannel_name, min_links, fallback): """Add port channel""" - error_map = {} - error_map[ValueError] = "{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO) - - db = validate(ctx.obj['db'], error_map, ctx) + db = validate(ctx.obj['db']) if is_portchannel_present_in_db(db, portchannel_name): ctx.fail("{} already exists!".format(portchannel_name)) @@ -1910,7 +1907,10 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback): fvs['min_links'] = str(min_links) if fallback != 'false': fvs['fallback'] = 'true' - db.set_entry('PORTCHANNEL', portchannel_name, fvs) + try: + db.set_entry('PORTCHANNEL', portchannel_name, fvs) + except ValueError: + ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO)) @portchannel.command('del') @click.argument('portchannel_name', metavar='', required=True) @@ -1918,16 +1918,15 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback): def remove_portchannel(ctx, portchannel_name): """Remove port channel""" - error_map = {} - error_map[ValueError] = "{} is invalid!, name should have prefix '{}' and suffix '{}'".format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO) - error_map[JsonPatchConflict] = "{} is not present.".format(portchannel_name) - - db = validate(ctx.obj['db'], error_map, ctx) + db = validate(ctx.obj['db']) if len([(k, v) for k, v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]) != 0: click.echo("Error: Portchannel {} contains members. Remove members before deleting Portchannel!".format(portchannel_name)) else: - db.set_entry('PORTCHANNEL', portchannel_name, None) + try: + db.set_entry('PORTCHANNEL', portchannel_name, None) + except JsonPatchConflict: + ctx.fail("{} is not present.".format(portchannel_name)) @portchannel.group(cls=clicommon.AbbreviationGroup, name='member') @click.pass_context diff --git a/config/validated_config_db_connector.py b/config/validated_config_db_connector.py index d72922908a..74799c69e9 100644 --- a/config/validated_config_db_connector.py +++ b/config/validated_config_db_connector.py @@ -2,7 +2,7 @@ from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat -def validate(config_db_connector, error_map, ctx=None): +def validate(config_db_connector): def validated_set_entry(table, key, value): if value: @@ -20,13 +20,7 @@ def validated_set_entry(table, key, value): gcu_patch = jsonpatch.JsonPatch(gcu_json_input) format = ConfigFormat.CONFIGDB.name config_format = ConfigFormat[format.upper()] - try: - GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) - except Exception as e: - if ctx: - ctx.fail(error_map[type(e)]) - else: - print("Case specific handling of errors, not all use ctx object") + GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) config_db_connector.set_entry = validated_set_entry return config_db_connector diff --git a/tests/portchannel_test.py b/tests/portchannel_test.py index 20ddae4c9d..31037cfbd4 100644 --- a/tests/portchannel_test.py +++ b/tests/portchannel_test.py @@ -23,7 +23,7 @@ def test_add_portchannel_with_invalid_name(self): print(result.exit_code) print(result.output) assert result.exit_code != 0 - assert 'does not satisfy the constraint "PortChannel[0-9]{1,4}"' in result.output + assert "Error: PortChan005 is invalid!, name should have prefix 'PortChannel' and suffix '<0-9999>'" in result.output def test_delete_portchannel_with_invalid_name(self): runner = CliRunner() @@ -35,8 +35,8 @@ def test_delete_portchannel_with_invalid_name(self): print(result.exit_code) print(result.output) assert result.exit_code != 0 - assert "can't remove a non-existent object" in result.output - + assert "Error: PortChan005 is not present." in result.output + def test_add_existing_portchannel_again(self): runner = CliRunner() db = Db() @@ -59,8 +59,8 @@ def test_delete_non_existing_portchannel(self): print(result.exit_code) print(result.output) assert result.exit_code != 0 - assert "can't remove a non-existent object" in result.output - + assert "Error: PortChannel0005 is not present." in result.output + def test_add_portchannel_member_with_invalid_name(self): runner = CliRunner() db = Db()