Skip to content

Commit 0a72d3b

Browse files
committed
Add headers to Auth0Error
1 parent ace28d8 commit 0a72d3b

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

auth0/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ def __init__(
1010
error_code: str,
1111
message: str,
1212
content: Any | None = None,
13+
headers: Any | None = None,
1314
) -> None:
1415
self.status_code = status_code
1516
self.error_code = error_code
1617
self.message = message
1718
self.content = content
19+
self.headers = headers
1820

1921
def __str__(self) -> str:
2022
return f"{self.status_code}: {self.message}"

auth0/rest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,14 @@ def content(self) -> Any:
296296
error_code=self._error_code(),
297297
message=self._error_message(),
298298
content=self._content,
299+
headers=self._headers
299300
)
300301

301302
raise Auth0Error(
302303
status_code=self._status_code,
303304
error_code=self._error_code(),
304305
message=self._error_message(),
306+
headers=self._headers
305307
)
306308
else:
307309
return self._content

auth0/test/authentication/test_get_token.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import requests
23
from fnmatch import fnmatch
34
from unittest import mock
45
from unittest.mock import ANY
@@ -335,7 +336,26 @@ def test_backchannel_login(self, mock_post):
335336
"grant_type": "urn:openid:params:grant-type:ciba",
336337
},
337338
)
338-
339+
340+
@mock.patch("auth0.rest.RestClient.post")
341+
def test_backchannel_login_headers_on_failure(self, mock_post):
342+
response = requests.Response()
343+
response.status_code = 400
344+
response.headers = {"Retry-After": 100}
345+
response._content = b'{"error":"slow_down"}'
346+
mock_post.side_effect = requests.exceptions.HTTPError(response=response)
347+
348+
g = GetToken("my.domain.com", "cid", client_secret="csec")
349+
350+
try:
351+
g.backchannel_login(
352+
auth_req_id="reqid",
353+
grant_type="urn:openid:params:grant-type:ciba",
354+
)
355+
except requests.exceptions.HTTPError as e:
356+
self.assertEqual(e.response.headers["Retry-After"], 100)
357+
self.assertEqual(e.response.status_code, 400)
358+
339359
@mock.patch("auth0.rest.RestClient.post")
340360
def test_connection_login(self, mock_post):
341361
g = GetToken("my.domain.com", "cid", client_secret="csec")
@@ -367,7 +387,7 @@ def test_connection_login(self, mock_post):
367387
)
368388

369389
@mock.patch("auth0.rest.RestClient.post")
370-
def test_connection_loginwith_login_hint(self, mock_post):
390+
def test_connection_login_with_login_hint(self, mock_post):
371391
g = GetToken("my.domain.com", "cid", client_secret="csec")
372392

373393
g.access_token_for_connection(

0 commit comments

Comments
 (0)