Skip to content

Commit

Permalink
refactor: exit commands with full exception message + add debug logs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
saranyailla authored Apr 7, 2023
1 parent db0962d commit 409df59
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 133 deletions.
3 changes: 2 additions & 1 deletion gdk/CLIParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def main():
args_namespace = cli_parser.parse_args()
parse_args_actions.run_command(args_namespace)
except Exception as e:
print(f"{utils.error_line}{e}")
print(f"{utils.error_line}")
logging.exception(e)
exit(1)


Expand Down
19 changes: 10 additions & 9 deletions gdk/aws_clients/Greengrassv2Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ def get_highest_component_version_(self) -> str:
if not component_versions:
return None
return component_versions[0]["componentVersion"]
except Exception as exc:
raise Exception(
f"Error while getting the component versions of '{component_name}' in '{region}' from the account"
f" '{account_num}' during publish.\n{exc}"
) from exc
except Exception:
logging.error(
"Error while getting the component versions of '%s' in '%s' from the account '%s' during publish.",
component_name,
region,
account_num,
)
raise

def _get_component_version(self, component_arn) -> dict:
comp_list_response = self.greengrass_client.list_component_versions(arn=component_arn)
Expand All @@ -57,8 +60,6 @@ def create_gg_component(self) -> None:
logging.info(
"Created private version '%s' of the component '%s' in the account.", component_version, component_name
)
except Exception as exc:
except Exception:
logging.error("Failed to create the component using the recipe at '%s'.", publish_recipe_file)
raise Exception(
f"Creating private version '{component_version}' of the component '{component_name}' failed.\n{exc}"
) from exc
raise
31 changes: 16 additions & 15 deletions gdk/aws_clients/S3Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def create_bucket(self, bucket, region):
else:
location = {"LocationConstraint": region}
self.s3_client.create_bucket(Bucket=bucket, CreateBucketConfiguration=location)
except Exception as exc:
except Exception:
logging.error("Failed to create the bucket '%s' in region '%s'", bucket, region)
raise Exception(exc) from exc
raise
logging.info("Successfully created the artifacts bucket '%s' in region '%s'", bucket, region)

def upload_artifacts(self, artifacts_to_upload):
Expand Down Expand Up @@ -68,8 +68,9 @@ def upload_artifacts(self, artifacts_to_upload):
s3_file_path = f"{component_name}/{component_version}/{artifact.name}"
logging.debug("Uploading artifact '%s' to the bucket '%s'.", artifact.resolve(), bucket)
self.s3_client.upload_file(str(artifact.resolve()), bucket, s3_file_path, ExtraArgs=s3_upload_file_args)
except Exception as exc:
raise Exception(f"Error while uploading the artifacts to s3 during publish.\n{exc}") from exc
except Exception:
logging.error("Failed to upload artifacts to s3 during publish.")
raise

def valid_bucket_for_artifacts_exists(self, bucket, region) -> bool:
location_constraint = None if region == "us-east-1" else region
Expand All @@ -84,16 +85,16 @@ def valid_bucket_for_artifacts_exists(self, bucket, region) -> bool:
except ClientError as exc:
error_code = exc.response["Error"]["Code"]
if error_code != "403" and error_code != "404":
raise Exception(
f"Could not verify if the bucket '{bucket}' exists in the region '{region}'.\nError:{exc}"
) from exc
logging.error("Could not verify if the bucket '%s' exists in the region '%s'.", bucket, region)
raise
elif error_code == "403":
raise Exception(
f"Bucket '{bucket}' already exists and is not owned by you. Please provide a different name for the"
" bucket in the configuration."
) from exc
logging.error(
"Bucket '%s' already exists and is not owned by you. Please provide a different name for the"
" bucket in the configuration.",
bucket,
)
raise
return False
except Exception as exc:
raise Exception(
f"Could not verify if the bucket '{bucket}' exists in the region '{region}'.\nError:{exc}"
) from exc
except Exception:
logging.error("Could not verify if the bucket '%s' exists in the region '%s'.", bucket, region)
raise
6 changes: 3 additions & 3 deletions gdk/build_system/Zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def build(self):
root_dir=artifacts_zip_build)
logging.debug("Archive complete.")

except Exception as e:
raise Exception(
"""Failed to zip the component in default build mode.\n{}""".format(e))
except Exception:
logging.error("Failed to zip the component in default build mode.")
raise

def get_ignored_file_patterns(self) -> list:
"""
Expand Down
10 changes: 6 additions & 4 deletions gdk/commands/component/BuildCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def default_build_component(self):
# Build the project with specified build system
self.run_build_command()
self.build_recipe_transformer.transform(self._get_build_folder_by_build_system())
except Exception as e:
raise Exception("""{}\n{}""".format(error_messages.BUILD_FAILED, e))
except Exception:
logging.error(error_messages.BUILD_FAILED)
raise

def get_build_cmd_from_platform(self, build_system):
"""
Expand Down Expand Up @@ -152,8 +153,9 @@ def run_build_command(self):
else:
logging.info("Running the build command '{}'".format(" ".join(build_command)))
sp.run(build_command, check=True)
except Exception as e:
raise Exception(f"Error building the component with the given build system.\n{e}")
except Exception:
logging.error("Error building the component with the given build system.")
raise

def _build_system_zip(self):
# Delegate to avoid breaking tests - TODO: We need to avoid testing private methods
Expand Down
18 changes: 9 additions & 9 deletions gdk/commands/component/InitCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ def init_with_template(self, template, language, project_dir):
template_name = "{}-{}".format(template, language)
logging.info("Fetching the component template '{}' from Greengrass Software Catalog.".format(template_name))
self.download_and_clean(template_name, "template", project_dir)
except Exception as e:
raise Exception("Could not initialize the project with component template '{}'.\n{}".format(template, e))
except Exception:
logging.error("Could not initialize the project with component template '%s'.", template)
raise

def init_with_repository(self, repository, project_dir):
try:
logging.info("Fetching the component repository '{}' from Greengrass Software Catalog.".format(repository))
self.download_and_clean(repository, "repository", project_dir)
except Exception as e:
raise Exception("Could not initialize the project with component repository '{}'.\n{}".format(repository, e))
except Exception:
logging.error("Could not initialize the project with component repository '%s'.", repository)
raise

def download_and_clean(self, comp_name, comp_type, project_dir):
"""
Expand All @@ -105,11 +107,9 @@ def download_and_clean(self, comp_name, comp_type, project_dir):
if download_response.status_code != 200:
try:
download_response.raise_for_status()
except Exception as e:
logging.error(e)
raise e
finally:
raise Exception(error_messages.INIT_FAILS_DURING_COMPONENT_DOWNLOAD.format(comp_type))
except Exception:
logging.error(error_messages.INIT_FAILS_DURING_COMPONENT_DOWNLOAD.format(comp_type))
raise

logging.debug("Downloading the component {}...".format(comp_type))
with zipfile.ZipFile(BytesIO(download_response.content)) as zfile:
Expand Down
10 changes: 5 additions & 5 deletions gdk/commands/component/ListCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def get_component_list_from_github(self, url) -> list:
response = requests.get(url)
try:
response.raise_for_status()
except Exception as e:
logging.error(e)
raise Exception(error_messages.LISTING_COMPONENTS_FAILED)
except Exception:
logging.error(error_messages.LISTING_COMPONENTS_FAILED)
raise

try:
return response.json()
Expand All @@ -49,8 +49,8 @@ def _map_template_name(self, template_name: str) -> str:
HelloWorld-python, HelloWorld-java.
"""
try:
language = re.search(r'\b(java|python)\b', template_name).group(1)
template_name = re.sub(r'\b\-(java|python)\b', '', template_name)
language = re.search(r"\b(java|python)\b", template_name).group(1)
template_name = re.sub(r"\b\-(java|python)\b", "", template_name)
return f"{template_name} ({language})"
except Exception:
return template_name
Expand Down
22 changes: 11 additions & 11 deletions gdk/commands/component/PublishCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import gdk.commands.component.component as component
import gdk.commands.component.project_utils as project_utils
import gdk.common.exceptions.error_messages as error_messages
import gdk.common.utils as utils
from gdk.aws_clients.Greengrassv2Client import Greengrassv2Client
from gdk.aws_clients.S3Client import S3Client
Expand All @@ -29,9 +28,9 @@ def run(self):
component_name = self.project_config["component_name"]
component_version = self.project_config["component_version"]
self._publish_component_version(component_name, component_version)
except Exception as e:
except Exception:
logging.error("Failed to publish new version of the component '{}'".format(self.project_config["component_name"]))
raise Exception("{}\n{}".format(error_messages.PUBLISH_FAILED, e))
raise

def try_build(self):
# TODO: This method should just warn and proceed. It should not build the component.
Expand Down Expand Up @@ -81,10 +80,9 @@ def _update_options(self):
if self.arguments.get("options"):
try:
self.project_config["options"] = self._read_options(self.arguments["options"])
except Exception as exc:
raise Exception(
"Please provide a valid json file path or a json string as the options argument.\nError:\t" + str(exc)
)
except Exception:
logging.error("Please provide a valid json file path or a json string as the options argument.")
raise

def _read_options(self, options):
try:
Expand Down Expand Up @@ -190,8 +188,9 @@ def get_next_version(self):
next_version = utils.get_next_patch_version(c_next_patch_version)
logging.info("Using '{}' as the next version of the component '{}' to create.".format(next_version, c_name))
return next_version
except Exception as e:
raise Exception("Failed to calculate the next version of the component during publish.\n{}".format(e))
except Exception:
logging.error("Failed to calculate the next version of the component during publish.")
raise

def get_account_number(self):
"""
Expand All @@ -212,8 +211,9 @@ def get_account_number(self):
account_num = caller_identity_response["Account"]
logging.debug("Identified account number as '{}'.".format(account_num))
return account_num
except Exception as e:
raise Exception("Error while fetching account number from credentials.\n{}".format(e))
except Exception:
logging.error("Error while fetching account number from credentials.")
raise

def get_component_version_from_config(self):
"""
Expand Down
22 changes: 4 additions & 18 deletions gdk/commands/component/component.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
def init(d_args):
from gdk.commands.component.InitCommand import InitCommand

try:
InitCommand(d_args).run()
except Exception as e:
raise Exception(f"Could not initialize the project due to the following error.\n{e}")
InitCommand(d_args).run()


def build(d_args):
from gdk.commands.component.BuildCommand import BuildCommand

try:
BuildCommand(d_args).run()
except Exception as e:
raise Exception(f"Could not build the project due to the following error.\n{e}")
BuildCommand(d_args).run()


def publish(d_args):
from gdk.commands.component.PublishCommand import PublishCommand

try:
PublishCommand(d_args).run()
except Exception as e:
raise Exception(f"Could not publish the component due to the following error.\n{e}")
PublishCommand(d_args).run()


def list(d_args):
from gdk.commands.component.ListCommand import ListCommand

try:
ListCommand(d_args).run()
except Exception as e:
raise Exception(
f"Could not list the available components from Greengrass Software Catalog due to the following error.\n{e}"
)
ListCommand(d_args).run()
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ def create_build_recipe_file(self, parsed_component_recipe) -> None:

component_recipe_file_name = self.project_config["component_recipe_file"].name
gg_build_recipe_file = Path(self.project_config["gg_build_recipes_dir"]).joinpath(component_recipe_file_name).resolve()
logging.debug("Creating component recipe at '%s'.", gg_build_recipe_file)
CaseInsensitiveRecipeFile().write(gg_build_recipe_file, parsed_component_recipe)
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ def create_publish_recipe_file(self, parsed_component_recipe) -> None:
"""
publish_recipe_file = Path(self.project_config["publish_recipe_file"]).resolve()
logging.debug("Creating component recipe at '%s'.", publish_recipe_file)
CaseInsensitiveRecipeFile().write(publish_recipe_file, parsed_component_recipe)
7 changes: 5 additions & 2 deletions integration_tests/gdk/components/test_integ_BuildCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def test_build_command_instantiation_failed_conflicting_args(mocker):
def test_build_run():
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "build"]))
assert "Could not build the project due to the following error." in e.value.args[0]
assert (
error_messages.CONFIG_FILE_NOT_EXISTS
in e.value.args[0]
)


def test_build_run_default_zip_json(mocker, supported_build_system, rglob_build_file):
Expand Down Expand Up @@ -284,7 +287,7 @@ def test_default_build_component_error_run_build_command(mocker, rglob_build_fil
)
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "build"]))
assert error_messages.BUILD_FAILED in e.value.args[0]
assert "err in run_build_command" in e.value.args[0]
assert mock_run_build_command.assert_called_once
assert not mock_transform.called

Expand Down
10 changes: 5 additions & 5 deletions integration_tests/gdk/components/test_integ_InitCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_init_run_with_conflicting_args(self):
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "init", "--repository", "dummy"]))

assert "Could not initialize the project due to the following error." in e.value.args[0]
assert "Some exception" in e.value.args[0]

assert mock_is_directory_empty.call_count == 0
assert mock_init_with_template.call_count == 0
Expand Down Expand Up @@ -212,7 +212,7 @@ def test_init_run_with_template_download_fail(self):
CLIParser.cli_parser.parse_args(["component", "init", "-t", "template", "-l", "python"])
)

assert "Could not initialize the project with component template 'template'." in e.value.args[0]
assert "error" in e.value.args[0]
assert mock_is_directory_empty.call_count == 1
assert mock_conflicting_args.call_count == 1
mock_download_and_clean.assert_called_once_with("template-python", "template", utils.current_directory)
Expand All @@ -225,7 +225,7 @@ def test_init_run_with_repository_download_fail(self):
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "init", "--repository", "dummy"]))

assert "Could not initialize the project with component repository 'dummy'." in e.value.args[0]
assert "error" in e.value.args[0]
assert mock_is_directory_empty.call_count == 1
assert mock_conflicting_args.call_count == 1
mock_download_and_clean.assert_called_once_with("dummy", "repository", utils.current_directory)
Expand All @@ -246,7 +246,7 @@ def test_init_with_template_invalid_url(self):
CLIParser.cli_parser.parse_args(["component", "init", "-t", "template", "-l", "python"])
)

assert "Failed to download the selected component" in e.value.args[0]
assert "some error" in e.value.args[0]
assert mock_template_download.call_count == 1
assert mock_is_directory_empty.call_count == 1
assert mock_conflicting_args.called
Expand All @@ -268,7 +268,7 @@ def test_init_with_repository_invalid_url(self):
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "init", "-r", "repo"]))

assert "Failed to download the selected component" in e.value.args[0]
assert "some error" in e.value.args[0]
assert mock_template_download.call_count == 1
assert mock_is_directory_empty.call_count == 1
assert mock_conflicting_args.called
Expand Down
5 changes: 2 additions & 3 deletions integration_tests/gdk/components/test_integ_ListCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import gdk.CLIParser as CLIParser
import gdk.common.consts as consts
import gdk.common.exceptions.error_messages as error_messages
import gdk.common.parse_args_actions as parse_args_actions
import pytest
from urllib3.exceptions import HTTPError
Expand All @@ -28,7 +27,7 @@ def test_list_template_failed_request(mocker):
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "list", "--template"]))
assert mock_list_req.called
assert error_messages.LISTING_COMPONENTS_FAILED in e.value.args[0]
assert "some error" in e.value.args[0]


def test_list_repository_failed_request(mocker):
Expand All @@ -37,7 +36,7 @@ def test_list_repository_failed_request(mocker):
with pytest.raises(Exception) as e:
parse_args_actions.run_command(CLIParser.cli_parser.parse_args(["component", "list", "--repository"]))
assert mock_list_req.called
assert error_messages.LISTING_COMPONENTS_FAILED in e.value.args[0]
assert "some error" in e.value.args[0]


def test_list_template_not_json(mocker):
Expand Down
Loading

0 comments on commit 409df59

Please sign in to comment.