From a47ca7978c951cc0f65fcd40114915c0a091031b Mon Sep 17 00:00:00 2001 From: thomas rusiecki Date: Thu, 7 Nov 2024 12:57:00 -0300 Subject: [PATCH] better error handling issue #21 --- evidence/forms.py | 8 ++++++-- evidence/management/commands/up_snapshots.py | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index dab7a60..05c9b82 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -37,8 +37,12 @@ class UploadForm(forms.Form): if exist_annotation: raise ValidationError("error: {} exist".format(file_name)) - except Exception: - raise ValidationError("error in: {}".format(file_name)) + except json.JSONDecodeError: + raise ValidationError("Error in parsing JSON: '{}'. Check for file integrity.".format(file_name)) + except ValidationError as e: + raise e + except Exception as e: + raise ValidationError("Oops! Something went wrong in '{}': {}".format(file_name, str(e))) self.evidences.append((file_name, file_json)) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index 3cb12fa..bea6ae1 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -41,10 +41,18 @@ class Command(BaseCommand): self.open(filepath) def open(self, filepath): - with open(filepath, 'r') as file: - content = json.loads(file.read()) - self.snapshots.append(content) - + try: + with open(filepath, 'r') as file: + content = json.loads(file.read()) + path_name = save_in_disk(content, self.user.institution.name) + + self.snapshots.append((content, path_name)) + except json.JSONDecodeError as e: + raise e + #or we cath'em all + except Exception: + raise Exception(f"Oops! Something went wrong there") + def parsing(self): for s in self.snapshots: self.devices.append(Build(s, self.user))