From 14ed0203ab564f9aa56eaf8ba9266f4ad6fa5580 Mon Sep 17 00:00:00 2001 From: HiyuP157 <296119922+HiyuP157@users.noreply.github.com> Date: Sun, 8 Nov 2026 06:28:31 +0000 Subject: [PATCH 1/2] fix(client): use HTTP reason for bodyless error responses Failed HEAD responses normally have no body, so Gitlab.http_request() left error_message empty and raised errors rendered as "404: ". Fall back to the response reason phrase, or the standard HTTPStatus phrase when the server did not provide one, only when no message could be parsed from the response. JSON response messages still take precedence, and response_body remains unchanged. Add regression coverage for the low-level HEAD path, ProjectFileManager.head(), and JSON message precedence. Fixes #3461 --- gitlab/client.py | 12 +++++++- tests/unit/objects/test_repositories.py | 21 +++++++++++++ tests/unit/test_gitlab_http_methods.py | 40 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/gitlab/client.py b/gitlab/client.py index f6fe89d84..9ddea7d46 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -4,6 +4,7 @@ import os import re +from http import HTTPStatus from typing import Any, BinaryIO, cast, TYPE_CHECKING from urllib import parse @@ -756,7 +757,7 @@ def http_request( ): continue - error_message = result.content + error_message: str | bytes = result.content try: error_json = result.json() for k in ("message", "error"): @@ -765,6 +766,15 @@ def http_request( except (KeyError, ValueError, TypeError): pass + if not error_message: + # Bodyless responses, such as HEAD errors, do not carry a + # GitLab error message. Use the HTTP reason phrase instead. + try: + default_reason = HTTPStatus(result.status_code).phrase + except ValueError: + default_reason = "" + error_message = result.reason or default_reason + if result.status_code == 401: raise gitlab.exceptions.GitlabAuthenticationError( response_code=result.status_code, diff --git a/tests/unit/objects/test_repositories.py b/tests/unit/objects/test_repositories.py index f891d4d09..859ae0db0 100644 --- a/tests/unit/objects/test_repositories.py +++ b/tests/unit/objects/test_repositories.py @@ -10,6 +10,7 @@ import responses from requests.structures import CaseInsensitiveDict +from gitlab.exceptions import GitlabHeadError from gitlab.v4.objects import ProjectFile file_path = "app/models/key.rb" @@ -62,6 +63,26 @@ def test_head_repository_file(project, resp_head_repository_file): assert headers["X-Gitlab-File-Path"] == file_path +@responses.activate +def test_head_repository_file_error_without_body(project): + missing_file_path = "nonexistent.yaml" + encoded_path = quote(missing_file_path, safe="") + responses.add( + method=responses.HEAD, + url=f"http://localhost/api/v4/projects/1/repository/files/{encoded_path}", + status=404, + ) + + with pytest.raises(GitlabHeadError) as exc_info: + project.files.head(missing_file_path, ref=ref) + + error = exc_info.value + assert error.response_code == 404 + assert error.response_body == b"" + assert error.error_message == "Not Found" + assert str(error) == "404: Not Found" + + @pytest.fixture def resp_get_repository_file(): file_response = { diff --git a/tests/unit/test_gitlab_http_methods.py b/tests/unit/test_gitlab_http_methods.py index 00d96d0be..04ab97caa 100644 --- a/tests/unit/test_gitlab_http_methods.py +++ b/tests/unit/test_gitlab_http_methods.py @@ -530,6 +530,46 @@ def test_head_request(gl): assert result["X-Total"] == "1" +@responses.activate +def test_head_request_error_without_body_uses_reason(gl): + url = "http://localhost/api/v4/not_there" + responses.add( + method=responses.HEAD, + url=url, + status=404, + match=helpers.MATCH_EMPTY_QUERY_PARAMS, + ) + + with pytest.raises(GitlabHttpError) as exc_info: + gl.http_head("/not_there") + + error = exc_info.value + assert error.response_code == 404 + assert error.response_body == b"" + assert error.error_message == "Not Found" + assert str(error) == "404: Not Found" + + +@responses.activate +def test_http_request_error_message_takes_precedence_over_reason(gl): + url = "http://localhost/api/v4/not_there" + responses.add( + method=responses.GET, + url=url, + json={"message": "File Not Found"}, + status=404, + match=helpers.MATCH_EMPTY_QUERY_PARAMS, + ) + + with pytest.raises(GitlabHttpError) as exc_info: + gl.http_get("/not_there") + + error = exc_info.value + assert error.response_body == b'{"message": "File Not Found"}' + assert error.error_message == "File Not Found" + assert str(error) == "404: File Not Found" + + @responses.activate def test_list_request(gl): url = "http://localhost/api/v4/projects" From d0cb40c9e6be0ed1df0fbd3252ce06c9a5d9dd7e Mon Sep 17 00:00:00 2001 From: HiyuP157 <296119922+HiyuP157@users.noreply.github.com> Date: Sun, 8 Nov 2026 07:04:57 +0000 Subject: [PATCH 2/2] test(client): cover unknown status fallback Exercise the defensive ValueError path used when the server returns a status code that is not part of http.HTTPStatus, so the fallback does not mask the original GitlabHttpError. --- tests/unit/test_gitlab_http_methods.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/unit/test_gitlab_http_methods.py b/tests/unit/test_gitlab_http_methods.py index 04ab97caa..706905572 100644 --- a/tests/unit/test_gitlab_http_methods.py +++ b/tests/unit/test_gitlab_http_methods.py @@ -550,6 +550,25 @@ def test_head_request_error_without_body_uses_reason(gl): assert str(error) == "404: Not Found" +@responses.activate +def test_head_request_error_with_unknown_status_preserves_error(gl): + url = "http://localhost/api/v4/not_there" + responses.add( + method=responses.HEAD, + url=url, + status=599, + match=helpers.MATCH_EMPTY_QUERY_PARAMS, + ) + + with pytest.raises(GitlabHttpError) as exc_info: + gl.http_head("/not_there") + + error = exc_info.value + assert error.response_code == 599 + assert error.response_body == b"" + assert str(error).startswith("599:") + + @responses.activate def test_http_request_error_message_takes_precedence_over_reason(gl): url = "http://localhost/api/v4/not_there"