Skip to content

Commit 51573dc

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 a3eafab commit 51573dc

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
@@ -95,6 +95,7 @@ def __init__(
9595
self.http_password = http_password
9696
self.oauth_token = oauth_token
9797
self.job_token = job_token
98+
self.auth_type = ""
9899
self._set_auth_info()
99100

100101
#: Create a session object for requests
@@ -405,21 +406,25 @@ def _set_auth_info(self) -> None:
405406
self.headers.pop("Authorization", None)
406407
self.headers["PRIVATE-TOKEN"] = self.private_token
407408
self.headers.pop("JOB-TOKEN", None)
409+
self.auth_type = "private_token"
408410

409411
if self.oauth_token:
410412
self.headers["Authorization"] = f"Bearer {self.oauth_token}"
411413
self.headers.pop("PRIVATE-TOKEN", None)
412414
self.headers.pop("JOB-TOKEN", None)
415+
self.auth_type = "oauth_token"
413416

414417
if self.job_token:
415418
self.headers.pop("Authorization", None)
416419
self.headers.pop("PRIVATE-TOKEN", None)
417420
self.headers["JOB-TOKEN"] = self.job_token
421+
self.auth_type = "job_token"
418422

419423
if self.http_username:
420424
self._http_auth = requests.auth.HTTPBasicAuth(
421425
self.http_username, self.http_password
422426
)
427+
self.auth_type = "password"
423428

424429
def enable_debug(self) -> None:
425430
import logging
@@ -640,6 +645,7 @@ def http_request(
640645
response_code=result.status_code,
641646
error_message=error_message,
642647
response_body=result.content,
648+
auth_type=self.auth_type,
643649
)
644650

645651
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",
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)