Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1239fd

Browse files
chore: add authentication type to GitlabAuthenticationError
Add the type of authentication used to the GitlabAuthenticationErrorexception. Hopefully this will make it easier to help user's debugauthentication issues they run into.
1 parentca58008 commitf1239fd

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
ifself.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
ifself.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
ifself.http_username:
502506
self._http_auth=requests.auth.HTTPBasicAuth(
503507
self.http_username,self.http_password
504508
)
509+
self.auth_type="password"
505510

506511
defenable_debug(self)->None:
507512
importlogging
@@ -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
raisegitlab.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
classGitlabAuthenticationError(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+
ifself.auth_type:
71+
returnf"{super().__str__()}: authentication_type:{self.auth_type}"
72+
returnsuper().__str__()
5673

5774

5875
classRedirectError(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
withpytest.raises(TestError)ascontext:
1717
raise_error_from_http_error()
1818
assertisinstance(context.value.__cause__,exceptions.GitlabHttpError)
19+
20+
21+
deftest_gitlabauthenticationerror_with_auth_type():
22+
withpytest.raises(exceptions.GitlabAuthenticationError)ascontext:
23+
raiseexceptions.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"instr(context.value)
30+
assert"job_token"instr(context.value)
31+
assert"401 Unauthorized"instr(context.value)
32+
33+
34+
deftest_gitlabauthenticationerror_no_auth_type():
35+
withpytest.raises(exceptions.GitlabAuthenticationError)ascontext:
36+
raiseexceptions.GitlabAuthenticationError(
37+
error_message="401 Unauthorized",
38+
response_code=401,
39+
response_body=b"bad user",
40+
)
41+
assert"authentication_type"notinstr(context.value)
42+
assert"401 Unauthorized"instr(context.value)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp