Skip to content

Commit 2bbc9c0

Browse files
authored
Fix AuthException message field format so it will include the full Descope-error object (#246)
1 parent 260b907 commit 2bbc9c0

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

descope/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from descope.exceptions import (
1212
API_RATE_LIMIT_RETRY_AFTER_HEADER,
1313
ERROR_TYPE_API_RATE_LIMIT,
14+
ERROR_TYPE_SERVER_ERROR,
1415
AuthException,
1516
RateLimitException,
1617
)

descope/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def _raise_from_response(self, response):
337337
raise AuthException(
338338
response.status_code,
339339
ERROR_TYPE_SERVER_ERROR,
340-
f"Error: {response.reason}",
340+
response.text,
341341
)
342342

343343
def _fetch_public_keys(self) -> None:

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from descope.exceptions import (
1212
API_RATE_LIMIT_RETRY_AFTER_HEADER,
1313
ERROR_TYPE_API_RATE_LIMIT,
14+
ERROR_TYPE_SERVER_ERROR,
1415
AuthException,
1516
RateLimitException,
1617
)

tests/test_auth.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from descope import (
88
API_RATE_LIMIT_RETRY_AFTER_HEADER,
99
ERROR_TYPE_API_RATE_LIMIT,
10+
ERROR_TYPE_SERVER_ERROR,
1011
AuthException,
1112
DeliveryMethod,
1213
RateLimitException,
@@ -582,6 +583,25 @@ def test_api_rate_limit_exception(self):
582583
{API_RATE_LIMIT_RETRY_AFTER_HEADER: 10},
583584
)
584585

586+
def test_raise_from_response(self):
587+
auth = Auth(self.dummy_project_id, self.public_key_dict)
588+
with patch("requests.get") as mock_request:
589+
mock_request.return_value.ok = False
590+
mock_request.return_value.status_code = 400
591+
mock_request.return_value.error_type = ERROR_TYPE_SERVER_ERROR
592+
mock_request.return_value.text = """{"errorCode":"E062108","errorDescription":"User not found","errorMessage":"Cannot find user","message":"Cannot find user"}"""
593+
with self.assertRaises(AuthException) as cm:
594+
auth.do_get(uri="http://test.com", params=False, allow_redirects=None)
595+
the_exception = cm.exception
596+
self.assertEqual(the_exception.status_code, 400)
597+
self.assertEqual(the_exception.error_type, ERROR_TYPE_SERVER_ERROR)
598+
with open("/tmp/ex.txt", "w") as f:
599+
f.write(the_exception.error_message)
600+
self.assertEqual(
601+
the_exception.error_message,
602+
"""{"errorCode":"E062108","errorDescription":"User not found","errorMessage":"Cannot find user","message":"Cannot find user"}""",
603+
)
604+
585605

586606
if __name__ == "__main__":
587607
unittest.main()

0 commit comments

Comments
 (0)