From b752a8750e9072d5531a8a96c688f0a370962da9 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Mon, 11 Nov 2024 04:06:22 -0300 Subject: [PATCH] better error messages --- evidence/forms.py | 20 ++++++++++++++------ evidence/management/commands/up_snapshots.py | 6 +++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index dab7a60..353bbf4 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -33,13 +33,21 @@ class UploadForm(forms.Form): exist_annotation = Annotation.objects.filter( uuid=file_json['uuid'] ).first() - + if exist_annotation: - raise ValidationError("error: {} exist".format(file_name)) - - except Exception: - raise ValidationError("error in: {}".format(file_name)) - + raise ValidationError( + _("The snapshot already exists"), + code="duplicate_snapshot", + params={"file_name": file_name}, + ) + + #Caught any error and display it as Validation Error so the Form handles it + except Exception as e: + raise ValidationError( + _("Error on '%(file_name)s': %(error)s"), + code="error", + params={"file_name": file_name, "error": getattr(e, 'message', str(e))}, + ) self.evidences.append((file_name, file_json)) return True diff --git a/evidence/management/commands/up_snapshots.py b/evidence/management/commands/up_snapshots.py index 5f699a7..760fab3 100644 --- a/evidence/management/commands/up_snapshots.py +++ b/evidence/management/commands/up_snapshots.py @@ -56,14 +56,14 @@ class Command(BaseCommand): except json.JSONDecodeError as e: logger.error("JSON decode error in file %s: %s", filepath, e) - raise ValueError(f"Invalid JSON format in file {filepath}") from e + raise ValueError(f"Invalid JSON format in file. Check for file integrity.") from e except FileNotFoundError as e: logger.error("File not found: %s", filepath) - raise FileNotFoundError(f"File not found: {filepath}") from e + raise FileNotFoundError(f"File not found") from e #or we cath'em all except Exception as e: logger.exception("Unexpected error when opening file %s: %s", filepath, e) - raise Exception(f"Unexpected error when opening file {filepath}") from e + raise Exception(f"Unexpected error when opening file") from e def parsing(self): for s, p in self.snapshots: