From abffb6c26ce5c47b896d34ab434ad3cd831f11be Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Wed, 29 Apr 2020 14:03:50 +0200 Subject: [PATCH] Don't crash when reading corrupt 3MF files Otherwise it would crash with a BadZipFile error. We should be robust against that. This will trigger a generic message that we couldn't read that file to the user, and put more information in the log. Fixes Sentry issue CURA-NH. --- plugins/3MFReader/ThreeMFWorkspaceReader.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index 82b73c66d9..f3c5a07f82 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -738,11 +738,15 @@ class ThreeMFWorkspaceReader(WorkspaceReader): @staticmethod def _loadMetadata(file_name: str) -> Dict[str, Dict[str, Any]]: - archive = zipfile.ZipFile(file_name, "r") + result = dict() + try: + archive = zipfile.ZipFile(file_name, "r") + except zipfile.BadZipFile: + Logger.logException("w", "Unable to retrieve metadata from {fname}: 3MF archive is corrupt.".format(fname = file_name)) + return result metadata_files = [name for name in archive.namelist() if name.endswith("plugin_metadata.json")] - result = dict() for metadata_file in metadata_files: try: