Skip to content

Commit b4fcd7d

Browse files
committed
Simplify logging exceptions
1 parent 2ebcce2 commit b4fcd7d

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

manage_breast_screening/dicom/tests/test_token_validator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_with_expired_signature(
6565
):
6666
validator = TokenValidator()
6767
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) is None
68-
mock_logger.error.assert_called_with("Token is expired", exc_info=True)
68+
mock_logger.exception.assert_called_with("Token is expired")
6969

7070
@patch(
7171
f"{TokenValidator.__module__}.jwt.decode", side_effect=jwt.InvalidAudienceError
@@ -75,8 +75,8 @@ def test_with_invalid_claims(
7575
):
7676
validator = TokenValidator()
7777
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) is None
78-
mock_logger.error.assert_called_with(
79-
"Invalid claims. Please check the audience and issuer.", exc_info=True
78+
mock_logger.exception.assert_called_with(
79+
"Invalid claims. Please check the audience and issuer."
8080
)
8181

8282
@patch(
@@ -87,8 +87,8 @@ def test_with_invalid_issuer(
8787
):
8888
validator = TokenValidator()
8989
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) is None
90-
mock_logger.error.assert_called_with(
91-
"Invalid claims. Please check the audience and issuer.", exc_info=True
90+
mock_logger.exception.assert_called_with(
91+
"Invalid claims. Please check the audience and issuer."
9292
)
9393

9494
@patch(f"{TokenValidator.__module__}.jwt.decode", side_effect=jwt.InvalidTokenError)
@@ -97,16 +97,16 @@ def test_with_invalid_token(
9797
):
9898
validator = TokenValidator()
9999
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) is None
100-
mock_logger.error.assert_called_with("Token is invalid", exc_info=True)
100+
mock_logger.exception.assert_called_with("Token is invalid")
101101

102102
@patch(f"{TokenValidator.__module__}.jwt.decode", side_effect=Exception)
103103
def test_with_unexpected_exception(
104104
self, mock_jwt_decode, mock_logger, mock_get_unverified_header, mock_urlopen
105105
):
106106
validator = TokenValidator()
107107
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) is None
108-
mock_logger.error.assert_called_with(
109-
"Unable to parse authentication token.", exc_info=True
108+
mock_logger.exception.assert_called_with(
109+
"Unable to parse authentication token."
110110
)
111111

112112
@patch(

manage_breast_screening/dicom/token_validator.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ def _decode(self, token: str, rsa_key: dict) -> dict | None:
6262
)
6363
return payload
6464
except jwt.ExpiredSignatureError:
65-
logger.error("Token is expired", exc_info=True)
65+
logger.exception("Token is expired")
6666
except (jwt.InvalidAudienceError, jwt.InvalidIssuerError):
67-
logger.error(
68-
"Invalid claims. Please check the audience and issuer.", exc_info=True
69-
)
67+
logger.exception("Invalid claims. Please check the audience and issuer.")
7068
except jwt.InvalidTokenError:
71-
logger.error("Token is invalid", exc_info=True)
69+
logger.exception("Token is invalid")
7270
except Exception:
73-
logger.error("Unable to parse authentication token.", exc_info=True)
71+
logger.exception("Unable to parse authentication token.")

0 commit comments

Comments
 (0)