Skip to content

Commit

Permalink
feat: better error handling for tw stdout and cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ejseqera committed Jun 28, 2023
1 parent 6afcae8 commit b678ed7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
10 changes: 5 additions & 5 deletions tw_pywrap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)


Expand Down
23 changes: 18 additions & 5 deletions tw_pywrap/tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b678ed7

Please sign in to comment.