From 6da6055e5b3133aea80be8344a4b26f5de25bb03 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Sun, 14 Jan 2024 20:56:31 +0000 Subject: [PATCH] Move exception handling of zipfile exception up on function call --- openandroidinstaller/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/openandroidinstaller/utils.py b/openandroidinstaller/utils.py index b6ecbd9a..8291a18e 100644 --- a/openandroidinstaller/utils.py +++ b/openandroidinstaller/utils.py @@ -84,9 +84,9 @@ def retrieve_image_metadata(image_path: str) -> dict: ].decode("utf-8") logger.info(f"Metadata retrieved from image {image_path.split('/')[-1]}.") return metadata_dict - except zipfile.BadZipFile: + except zipfile.BadZipFile as e: logger.error("Selected image is not a zip file.") - return dict + raise e except (FileNotFoundError, KeyError): logger.error( f"Metadata file {metapath} not found in {image_path.split('/')[-1]}." @@ -132,8 +132,8 @@ def image_works_with_device( Returns: CheckResult object containing the compatibility status and a message. """ - metadata = retrieve_image_metadata(image_path) try: + metadata = retrieve_image_metadata(image_path) supported_devices = metadata["pre-device"].split(",") logger.info(f"Image works with the following device(s): {supported_devices}") if any(code in supported_devices for code in supported_device_codes): @@ -148,6 +148,11 @@ def image_works_with_device( CompatibilityStatus.INCOMPATIBLE, f"Image file {image_path.split('/')[-1]} is not supported by device code.", ) + except zipfile.BadZipFile: + return CheckResult( + CompatibilityStatus.INCOMPATIBLE, + f"Selected image {image_path.split('/')[-1]} is not a zip file.", + ) except KeyError: logger.error( f"Could not determine supported devices for {image_path.split('/')[-1]}."