Skip to content

Commit

Permalink
Fix SanicException quiet attribute handling when set to False (#3009)
Browse files Browse the repository at this point in the history
Fixes an issue where the `quiet` attribute in `SanicException` would default to the class attribute when set to `False`.
This occurs when the subclass `quiet` attribute is `True`. The fix ensures only `None` triggers the fallback.

Co-authored-by: Adam Hopkins <[email protected]>
  • Loading branch information
C5H12O5 and ahopkins authored Dec 16, 2024
1 parent cce077c commit 665234d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sanic/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 72,11 @@ def __init__(
status_code = status_code or getattr(
self.__class__, "status_code", None
)
quiet = quiet or getattr(self.__class__, "quiet", None)
quiet = (
quiet
if quiet is not None
else getattr(self.__class__, "quiet", None)
)
headers = headers or getattr(self.__class__, "headers", {})
if message is None:
message = self.message
Expand Down
15 changes: 15 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 417,21 @@ class CustomError(SanicException):
assert SanicException("").message == ""


def test_exception_quiet_attribute():
class SilentException(SanicException):
quiet = True

class NoisyException(SanicException):
quiet = False

assert SilentException().quiet
assert not NoisyException().quiet
assert SilentException(quiet=True).quiet
assert NoisyException(quiet=True).quiet
assert not SilentException(quiet=False).quiet
assert not NoisyException(quiet=False).quiet


def test_request_middleware_exception_on_404(app: Sanic):
"""See https://github.com/sanic-org/sanic/issues/2950"""
counter = count()
Expand Down

0 comments on commit 665234d

Please sign in to comment.