8000 chore: add authentication type to GitlabAuthenticationError · python-gitlab/python-gitlab@cce2202 · GitHub
[go: up one dir, main page]

Skip to content

Commit cce2202

Browse files
chore: add authentication type to GitlabAuthenticationError
Add the type of authentication used to the GitlabAuthenticationError exception. Hopefully this will make it easier to help user's debug authentication issues they run into.
1 parent c6d7e9a commit cce2202

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

gitlab/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def __init__(
9696
self.http_password = http_password
9797
self.oauth_token = oauth_token
9898
self.job_token = job_token
99+
self.auth_type = ""
99100
self._set_auth_info()
100101

101102
#: Create a session object for requests
@@ -487,21 +488,25 @@ def _set_auth_info(self) -> None:
487488
self.headers.pop("Authorization", None)
488489
self.headers["PRIVATE-TOKEN"] = self.private_token
489490
self.headers.pop("JOB-TOKEN", None)
491+
self.auth_type = "private_token"
490492

491493
if self.oauth_token:
492494
self.headers["Authorization"] = f"Bearer {self.oauth_token}"
493495
self.headers.pop("PRIVATE-TOKEN", None)
494496
self.headers.pop("JOB-TOKEN", None)
497+
self.auth_type = "oauth_token"
495498

496499
if self.job_token:
497500
self.headers.pop("Authorization", None)
498501
self.headers.pop("PRIVATE-TOKEN", None)
499502
self.headers["JOB-TOKEN"] = self.job_token
503+
self.auth_type = "job_token"
500504

501505
if self.http_username:
502506
self._http_auth = requests.auth.HTTPBasicAuth(
503507
self.http_username, self.http_password
504508
)
509+
self.auth_type = "password"
505510

506511
def enable_debug(self) -> None:
507512
import logging
@@ -722,6 +727,7 @@ def http_request(
722727
response_code=result.status_code,
723728
error_message=error_message,
724729
response_body=result.content,
730+
auth_type=self.auth_type,
725731
)
726732

727733
raise gitlab.exceptions.GitlabHttpError(

gitlab/exceptions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,24 @@ def __str__(self) -> str:
5252

5353

5454
class GitlabAuthenticationError(GitlabError):
55-
pass
55+
def __init__(
56+
self,
57+
error_message: Union[str, bytes] = "",
58+
response_code: Optional[int] = None,
59+
response_body: Optional[bytes] = None,
60+
auth_type: str = "",
61+
) -> None:
62+
super().__init__(
63+
error_message=error_message,
64+
response_code=response_code,
65+
response_body=response_body,
66+
)
67+
self.auth_type = auth_type
68+
69+
def __str__(self) -> str:
70+
if self.auth_type:
71+
return f"{super().__str__()}: authentication_type: {self.auth_type}"
72+
return super().__str__()
5673

5774

5875
class RedirectError(GitlabError):

tests/unit/test_exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,27 @@ def raise_error_from_http_error():
1616
with pytest.raises(TestError) as context:
1717
raise_error_from_http_error()
1818
assert isinstance(context.value.__cause__, exceptions.GitlabHttpError)
19+
20+
21+
def test_gitlabauthenticationerror_with_auth_type():
22+
with pytest.raises(exceptions.GitlabAuthenticationError) as context:
23+
raise exceptions.GitlabAuthenticationError(
24+
error_message="401 Unauthorized",
25+
response_code=401,
26+
response_body=b"bad user",
27+
auth_type="job_token" 8A1A ,
28+
)
29+
assert "authentication_type" in str(context.value)
30+
assert "job_token" in str(context.value)
31+
assert "401 Unauthorized" in str(context.value)
32+
33+
34+
def test_gitlabauthenticationerror_no_auth_type():
35+
with pytest.raises(exceptions.GitlabAuthenticationError) as context:
36+
raise exceptions.GitlabAuthenticationError(
37+
error_message="401 Unauthorized",
38+
response_code=401,
39+
response_body=b"bad user",
40+
)
41+
assert "authentication_type" not in str(context.value)
42+
assert "401 Unauthorized" in str(context.value)

0 commit comments

Comments
 (0)
0