Skip to content

Commit d7915bf

Browse files
committed
Fix SanicException quiet attribute handling when set to False
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.
1 parent da1c646 commit d7915bf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

sanic/exceptions.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def __init__(
7272
status_code = status_code or getattr(
7373
self.__class__, "status_code", None
7474
)
75-
quiet = quiet or getattr(self.__class__, "quiet", None)
75+
quiet = (
76+
quiet
77+
if quiet is not None
78+
else getattr(self.__class__, "quiet", None)
79+
)
7680
headers = headers or getattr(self.__class__, "headers", {})
7781
if message is None:
7882
message = self.message

tests/test_exceptions.py

+15
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ class CustomError(SanicException):
417417
assert SanicException("").message == ""
418418

419419

420+
def test_exception_quiet_attribute():
421+
class SilentException(SanicException):
422+
quiet = True
423+
424+
class NoisyException(SanicException):
425+
quiet = False
426+
427+
assert SilentException().quiet
428+
assert not NoisyException().quiet
429+
assert SilentException(quiet=True).quiet
430+
assert NoisyException(quiet=True).quiet
431+
assert not SilentException(quiet=False).quiet
432+
assert not NoisyException(quiet=False).quiet
433+
434+
420435
def test_request_middleware_exception_on_404(app: Sanic):
421436
"""See https://github.com/sanic-org/sanic/issues/2950"""
422437
counter = count()

0 commit comments

Comments
 (0)