diff --git a/tw_pywrap/cli.py b/tw_pywrap/cli.py index e4da3bd..ec26ac1 100644 --- a/tw_pywrap/cli.py +++ b/tw_pywrap/cli.py @@ -9,9 +9,9 @@ import yaml from pathlib import Path -from tw_pywrap import tower -import tw_pywrap.helper as helper # TODO: refactor -from tw_pywrap import overwrite +from tw_pywrap import tower, helper, overwrite +from tw_pywrap.tower import ResourceCreationError, ResourceExistsError + logger = logging.getLogger(__name__) @@ -113,10 +113,10 @@ def main(): # Run the 'tw' methods for each block block_manager.handle_block(block, args) time.sleep(3) - except Exception as e: + except ResourceExistsError as e: logging.error(e) continue - except Exception as e: + except ResourceCreationError as e: logging.error(e) diff --git a/tw_pywrap/tower.py b/tw_pywrap/tower.py index a873c82..26bdf3f 100644 --- a/tw_pywrap/tower.py +++ b/tw_pywrap/tower.py @@ -62,12 +62,17 @@ def _tw_run(self, cmd, *args, **kwargs): stdout, _ = process.communicate() stdout = stdout.decode("utf-8").strip() - # Check if an error occurred - # ruff: noqa: E501 + # Error handling for stdout if stdout: - if re.search(r"ERROR: .* already exists", stdout): - logging.debug( - " Resource already exists and will not be created. Please set 'overwrite: true'\n" + if re.search(r"ERROR: (?!A pipeline).* already exists", stdout): + raise ResourceExistsError( + " Resource already exists and will not be created." + "Please set 'overwrite: true'\n" + ) + elif re.search(r"ERROR: .*", stdout): + raise ResourceCreationError( + f" Resource creation failed with the following error: '{stdout}'.\n" + "Please check your config file and try again.\n" ) else: return stdout @@ -80,3 +85,11 @@ def __getattr__(self, cmd): """ cmd = cmd.replace("_", "-") # replace underscores with hyphens return self.TwCommand(self, cmd) + + +class ResourceExistsError(Exception): + pass + + +class ResourceCreationError(Exception): + pass