added exceptions for json parsing erro

This commit is contained in:
Thomas Nahuel Rusiecki 2024-11-02 04:21:01 -03:00
parent e4124fb20b
commit 7d4ef21cef
2 changed files with 13 additions and 5 deletions

View File

@ -37,6 +37,8 @@ class UploadForm(forms.Form):
if exist_annotation: if exist_annotation:
raise ValidationError("error: {} exist".format(file_name)) raise ValidationError("error: {} exist".format(file_name))
except ValueError:
raise ValidationError("Error in parsing JSON: {}. Check for file corruption.".format(file_name))
except Exception: except Exception:
raise ValidationError("error in: {}".format(file_name)) raise ValidationError("error in: {}".format(file_name))

View File

@ -47,10 +47,16 @@ class Command(BaseCommand):
self.open(filepath) self.open(filepath)
def open(self, filepath): def open(self, filepath):
try:
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
content = json.loads(file.read()) content = json.loads(file.read())
path_name = save_in_disk(content, self.user.institution.name) path_name = save_in_disk(content, self.user.institution.name)
self.snapshots.append((content, path_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): def parsing(self):
for s, p in self.snapshots: for s, p in self.snapshots: