diff options
| author | John Estabrook <jestabro@vyos.io> | 2025-10-27 15:13:16 -0500 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2025-11-05 07:49:39 -0600 |
| commit | 42015261ce2e83d79b82c5a52bddcdbb0ee67592 (patch) | |
| tree | a7a8653dccce83cd894a0dc2485e6115deeca627 /python | |
| parent | 47cc7ab10b9b6bc6fe21025440687a020bd16e35 (diff) | |
| download | vyos-1x-42015261ce2e83d79b82c5a52bddcdbb0ee67592.tar.gz vyos-1x-42015261ce2e83d79b82c5a52bddcdbb0ee67592.zip | |
T7915: minor fixes for consistent exception handling and error messages
The addition of alert exn to vyos1x-config allows an audit of all raised
exceptions, providing fixes for consistent handling and error reporting.
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/configtree.py | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 21b37e638..e6ea4b40d 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -264,7 +264,8 @@ class ConfigTree(object): res = self.__create_node(self.__config, path_str) if res != 0: - raise ConfigTreeError(f'Path already exists: {path}') + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: {path}') def set(self, path, value=None, replace=True): """Set new entry in VyOS configuration. @@ -279,12 +280,20 @@ class ConfigTree(object): path_str = ' '.join(map(str, path)).encode() if value is None: - self.__set_valueless(self.__config, path_str) + res = self.__set_valueless(self.__config, path_str) else: if replace: - self.__set_replace_value(self.__config, path_str, str(value).encode()) + res = self.__set_replace_value( + self.__config, path_str, str(value).encode() + ) else: - self.__set_add_value(self.__config, path_str, str(value).encode()) + res = self.__set_add_value(self.__config, path_str, str(value).encode()) + + if res != 0: + msg = self.__get_error().decode() + raise ConfigTreeError( + f'{msg}: path "{path}" value "{value}" replace "{replace}"' + ) if self.__migration: self.migration_log.info( @@ -297,7 +306,8 @@ class ConfigTree(object): res = self.__delete(self.__config, path_str) if res != 0: - raise ConfigTreeError(f"Path doesn't exist: {path}") + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: path "{path}"') if self.__migration: self.migration_log.info(f'- op: delete path: {path}') @@ -308,12 +318,8 @@ class ConfigTree(object): res = self.__delete_value(self.__config, path_str, value.encode()) if res != 0: - if res == 1: - raise ConfigTreeError(f"Path doesn't exist: {path}") - elif res == 2: - raise ConfigTreeError(f"Value doesn't exist: '{value}'") - else: - raise ConfigTreeError() + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: path "{path}" value "{value}"') if self.__migration: self.migration_log.info(f'- op: delete_value path: {path} value: {value}') @@ -326,10 +332,12 @@ class ConfigTree(object): # Check if a node with intended new name already exists new_path = path[:-1] + [new_name] if self.exists(new_path): - raise ConfigTreeError() + raise ConfigTreeError(f'Name {new_name} already exists') + res = self.__rename(self.__config, path_str, newname_str) if res != 0: - raise ConfigTreeError("Path [{}] doesn't exist".format(path)) + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: {path}') if self.__migration: self.migration_log.info( @@ -369,7 +377,7 @@ class ConfigTree(object): check_path(path) path_str = ' '.join(map(str, path)).encode() - res = self.__value_exists(self.__config, path_str, value) + res = self.__value_exists(self.__config, path_str, value.encode()) if res == 0: return False else: @@ -432,13 +440,18 @@ class ConfigTree(object): if res == 0: return True else: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: {path}') def is_leaf(self, path): check_path(path) path_str = ' '.join(map(str, path)).encode() - return self.__is_leaf(self.__config, path_str) + res = self.__is_leaf(self.__config, path_str) + if res >= 1: + return True + else: + return False def set_leaf(self, path, value): check_path(path) @@ -448,7 +461,8 @@ class ConfigTree(object): if res == 0: return True else: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) + msg = self.__get_error().decode() + raise ConfigTreeError(f'{msg}: {path}') def get_subtree(self, path, with_node=False): check_path(path) |
