Skip to content

Commit 4b55951

Browse files
committed
fix(vcs): improve GitLab error handling
Show the actual error from the request. Fixes WEBLATE-39D7
1 parent ebb0144 commit 4b55951

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

weblate/vcs/git.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,12 +2120,21 @@ def get_headers(self, credentials: GitCredentials) -> dict[str, str]:
21202120
return headers
21212121

21222122
def get_target_project_id(self, credentials: GitCredentials):
2123-
response_data, _response, error = self.request(
2123+
response_data, response, error = self.request(
21242124
"get", credentials, credentials["url"]
21252125
)
21262126
if "id" not in response_data:
2127-
report_error("Could not get project", message=True)
2128-
raise RepositoryError(0, f"Could not get project: {error}")
2127+
detail = error or response.reason or gettext("Unknown error")
2128+
report_error(
2129+
"Could not get GitLab project",
2130+
message=True,
2131+
extra_log=f"{response.status_code}: {detail}",
2132+
)
2133+
raise RepositoryError(
2134+
0,
2135+
gettext("Could not get GitLab project (%(status)s): %(error)s")
2136+
% {"status": response.status_code, "error": detail},
2137+
)
21292138
return response_data["id"]
21302139

21312140
def configure_fork_features(

weblate/vcs/tests/test_vcs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,33 @@ def test_push_rejected(self, branch: str = "") -> None:
20392039
)
20402040
self.assertEqual(call_count, 1)
20412041

2042+
@responses.activate
2043+
def test_get_target_project_id_auth_error(self) -> None:
2044+
responses.add(
2045+
responses.GET,
2046+
"https://gitlab.com/api/v4/projects/WeblateOrg%2Ftest",
2047+
json={
2048+
"error": "invalid_token",
2049+
"error_description": "Token is expired.",
2050+
},
2051+
status=401,
2052+
)
2053+
2054+
with (
2055+
patch("weblate.vcs.git.report_error") as mock_report_error,
2056+
self.assertRaisesMessage(
2057+
RepositoryError,
2058+
"Could not get GitLab project (401): invalid_token, Token is expired.",
2059+
),
2060+
):
2061+
self.repo.get_target_project_id(self.repo.get_credentials())
2062+
2063+
mock_report_error.assert_called_once_with(
2064+
"Could not get GitLab project",
2065+
message=True,
2066+
extra_log="401: invalid_token, Token is expired.",
2067+
)
2068+
20422069
@responses.activate
20432070
def test_pull_request_error(self, branch: str = "") -> None:
20442071
# Patch push_to_fork() function because we don't want to actually

0 commit comments

Comments
 (0)