Skip to content

Commit

Permalink
Add a summary property to ValidationError exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Apr 11, 2024
1 parent b489675 commit c2b356d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions fedora_messaging/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Exceptions raised by Fedora Messaging."""

import jsonschema


class BaseException(Exception):
"""The base class for all exceptions raised by fedora_messaging."""
Expand Down Expand Up @@ -182,3 +184,11 @@ class ValidationError(BaseException):
and testing that you're trying to publish a message with a different
format, and that you should either fix it or update the schema.
"""

@property
def summary(self):
"""A short summary of the error."""
original_exception = self.args[0]
if isinstance(original_exception, jsonschema.exceptions.ValidationError):
return original_exception.message
return str(original_exception)
1 change: 1 addition & 0 deletions news/+validation-summary.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a `summary` property to `ValidationError` exceptions
6 changes: 4 additions & 2 deletions tests/unit/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def test_invalid_severity(self):
msg = message.Message()
msg._headers["fedora_messaging_severity"] = 42

with pytest.raises(exceptions.ValidationError):
with pytest.raises(exceptions.ValidationError) as excinfo:
message.get_message("", msg._properties, b"{}")
assert excinfo.value.summary == "42 is not one of [10, 20, 30, 40]"

def test_missing_headers(self):
"""Assert missing headers results in a default message."""
Expand Down Expand Up @@ -140,8 +141,9 @@ def test_proper_message_multiple(self):
def test_improper_messages(self):
"""Assert TypeError is raised when improper messages are provided"""
messages = ["m1", "m2"]
with pytest.raises(exceptions.ValidationError):
with pytest.raises(exceptions.ValidationError) as excinfo:
message.dumps(messages)
assert excinfo.value.summary == "'str' object has no attribute 'validate'"


class TestMessageLoads:
Expand Down

0 comments on commit c2b356d

Please sign in to comment.