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)