Repository navigation
python(fix): include server error message in sift_client HTTP errors #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wei-sift
merged 3 commits into
main
from
eng-16487-include-server-error-message-in-sift_client-upload-failures
Nov 4, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Data import and other file upload and download errors now include the server's error message. File attachment, artifact, and test result uploads raise `requests.HTTPError` instead of `Exception`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from requests import HTTPError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from requests import Response | ||
|
|
||
|
|
||
| def raise_for_status(response: Response) -> None: | ||
| """Like ``Response.raise_for_status``, with the server's response body in the message. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the response has a 4xx or 5xx status. | ||
| """ | ||
| try: | ||
| response.raise_for_status() | ||
| except HTTPError as error: | ||
| body = response.text.strip() | ||
| if body: | ||
| raise HTTPError(f"{error}\nServer response: {body}", response=response) from error | ||
| raise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from requests import HTTPError, Request, Response | ||
|
|
||
| from sift_client._internal.util.http import raise_for_status | ||
|
|
||
| URL = "https://example.test/api/v2/data-imports:upload/import-id" | ||
|
|
||
|
|
||
| def _response(status_code: int, reason: str, body: bytes) -> Response: | ||
| response = Response() | ||
| response.status_code = status_code | ||
| response.reason = reason | ||
| response.url = URL | ||
| response.request = Request("POST", URL).prepare() | ||
| response.encoding = "utf-8" | ||
| response._content = body | ||
| return response | ||
|
|
||
|
|
||
| class TestRaiseForStatus: | ||
| @pytest.mark.parametrize( | ||
| ("status_code", "reason", "body"), | ||
| [ | ||
| ( | ||
| 400, | ||
| "Bad Request", | ||
| 'failed to parse row 20: parsing time "10-29-2026 16:53:45" as ' | ||
| '"2006-01-02 15:04:05": cannot parse "10-29-2026 16:53:45" as "2006"', | ||
| ), | ||
| (500, "Internal Server Error", "upload failed\n"), | ||
| (502, "Bad Gateway", "<html>Bad Gateway</html>"), | ||
| ], | ||
| ) | ||
| def test_error_includes_server_body(self, status_code, reason, body): | ||
| response = _response(status_code, reason, body.encode("utf-8")) | ||
|
|
||
| with pytest.raises(HTTPError) as exc_info: | ||
| raise_for_status(response) | ||
|
|
||
| message = str(exc_info.value) | ||
| assert body.strip() in message | ||
| assert str(status_code) in message | ||
| assert reason in message | ||
| assert URL in message | ||
| assert exc_info.value.response is response | ||
| assert exc_info.value.request is response.request | ||
|
|
||
| @pytest.mark.parametrize("body", [b"", b" \n\t"]) | ||
| def test_empty_body_preserves_http_error(self, body): | ||
| response = _response(400, "Bad Request", body) | ||
|
|
||
| with pytest.raises(HTTPError) as exc_info: | ||
| raise_for_status(response) | ||
|
|
||
| assert str(exc_info.value) == f"400 Client Error: Bad Request for url: {URL}" | ||
| assert exc_info.value.response is response | ||
|
|
||
| def test_success_does_not_raise(self): | ||
| raise_for_status(_response(200, "OK", b'{"jobId":"job-id"}')) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.