From 79951f3c4b4054dfb6b5a2583778568147fe7cd9 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 05:28:39 -0300 Subject: [PATCH] better error handling on submit form --- evidence/forms.py | 12 +++++++----- evidence/management/commands/up_snapshots.py | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index 29876f3..fefe45a 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -35,12 +35,14 @@ class UploadForm(forms.Form): ).first() if exist_annotation: - raise ValidationError("error: {} exist".format(file_name)) + raise ValidationError("Error! Snapshot: {} already exists".format(file_name)) - except ValueError: - raise ValidationError("Error in parsing JSON: {}. Check for file corruption.".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 2f72f71..f2dbb22 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -53,13 +53,15 @@ class Command(BaseCommand): 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: - raise ValueError(f"Invalid JSON format in file {filepath}.") + except json.JSONDecodeError as e: + raise e #or we cath'em all - except Exception as e: + except Exception: raise Exception(f"Oops! Something went wrong there") + def parsing(self): for s, p in self.snapshots: try: