From 7d4ef21cef3aa716fa5edb964d0da0ce32f76957 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 04:21:01 -0300 Subject: [PATCH 1/5] added exceptions for json parsing erro --- evidence/forms.py | 2 ++ evidence/management/commands/up_snapshots.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index dab7a60..29876f3 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -37,6 +37,8 @@ class UploadForm(forms.Form): if exist_annotation: raise ValidationError("error: {} exist".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)) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index 69ac134..ca940c8 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -47,13 +47,19 @@ class Command(BaseCommand): self.open(filepath) def open(self, filepath): - 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)) + 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: + raise ValueError(f"Invalid JSON format in file {filepath}.") + #or we cath'em all + except Exception as e: + raise Exception(f"Oops! Something went wrong there") def parsing(self): - for s, p in self.snapshots: + for s, p in self.snapshots: try: self.devices.append(Build(s, self.user)) move_json(p, self.user.institution.name) -- 2.30.2 From 7653c9d5f93ed16d06c24b2f4877ceaba53ea1f9 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 04:30:58 -0300 Subject: [PATCH 2/5] check that websnapshot has values --- device/templates/details.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/device/templates/details.html b/device/templates/details.html index 8477948..80c5a21 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -58,7 +58,7 @@
{{ object.type }}
- {% if object.is_websnapshot %} + {% if object.is_websnapshot and object.last_user_evidence %} {% for k, v in object.last_user_evidence %}
{{ k }}
-- 2.30.2 From 3faa98a60e4f136254d89793af76da4464a87536 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 04:46:37 -0300 Subject: [PATCH 3/5] exception for file path --- evidence/management/commands/up_snapshots.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index ca940c8..f966201 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -37,6 +37,8 @@ class Command(BaseCommand): elif os.path.isdir(path): self.read_directory(path) + else: + raise ValueError(f"The path {path} is neither a file nor a directory") self.parsing() -- 2.30.2 From f8beb0c6e6bd03b3a9f9b6d92509ceb6a9c0f14c Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 05:02:09 -0300 Subject: [PATCH 4/5] fixed self inflicted bad identation --- evidence/management/commands/up_snapshots.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index f966201..2f72f71 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -61,7 +61,7 @@ class Command(BaseCommand): raise Exception(f"Oops! Something went wrong there") def parsing(self): - for s, p in self.snapshots: + for s, p in self.snapshots: try: self.devices.append(Build(s, self.user)) move_json(p, self.user.institution.name) -- 2.30.2 From 79951f3c4b4054dfb6b5a2583778568147fe7cd9 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Sat, 2 Nov 2024 05:28:39 -0300 Subject: [PATCH 5/5] 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: -- 2.30.2