Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Security

### Bug Fixes
* Fixes TypeError in _unknown_error() when API returns unparseable error on streaming request (#1264)

### Documentation

Expand Down
2 changes: 1 addition & 1 deletion databricks/sdk/errors/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _unknown_error(response: requests.Response, debug_headers: bool = False) ->
return (
"This is likely a bug in the Databricks SDK for Python or the underlying "
"API. Please report this issue with the following debugging information to the SDK issue tracker at "
f"https://github.com/databricks/databricks-sdk-go/issues. Request log:```{request_log}```"
f"https://github.com/databricks/databricks-sdk-py/issues. Request log:```{request_log}```"
)


Expand Down
5 changes: 4 additions & 1 deletion databricks/sdk/logger/round_trip_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def generate(self) -> str:
for k, v in request.headers.items():
sb.append(f"> * {k}: {self._only_n_bytes(v, self._debug_truncate_bytes)}")
if request.body:
sb.append("> [raw stream]" if self._raw else self._redacted_dump("> ", request.body))
if self._raw or not isinstance(request.body, str):
sb.append("> [raw stream]")
else:
sb.append(self._redacted_dump("> ", request.body))
sb.append(f"< {self._response.status_code} {self._response.reason}")
if self._raw and self._response.headers.get("Content-Type", None) != "application/json":
# Raw streams with `Transfer-Encoding: chunked` do not have `Content-Type` header
Expand Down
29 changes: 26 additions & 3 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class TestCase:
want_message=(
"unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. "
"Please report this issue with the following debugging information to the SDK issue tracker at "
"https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n"
"https://github.com/databricks/databricks-sdk-py/issues. Request log:```GET /api/2.0/service\n"
"< 400 Bad Request\n"
"< this is not a real response```"
),
Expand Down Expand Up @@ -343,7 +343,7 @@ class TestCase:
response_body=json.dumps("This is JSON but not a dictionary"),
),
want_err_type=errors.NotFound,
want_message='unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< "This is JSON but not a dictionary"```',
want_message='unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-py/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< "This is JSON but not a dictionary"```',
),
TestCase(
name="unable_to_parse_response3",
Expand All @@ -353,7 +353,7 @@ class TestCase:
response_body=b"\x80",
),
want_err_type=errors.NotFound,
want_message="unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< �```",
want_message="unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-py/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< �```",
),
]

Expand Down Expand Up @@ -415,3 +415,26 @@ def test_debug_headers_enabled_shows_headers():
assert "debug-token-12345" in error_message
assert "X-Databricks-Azure-SP-Management-Token" in error_message
assert "debug-azure-token-67890" in error_message


def test_unknown_error_with_bytesio_request_body():
"""Test that _unknown_error handles BytesIO request bodies without crashing.

Regression test for https://github.com/databricks/databricks-sdk-py/issues/1264
"""
import io

resp = requests.Response()
resp.status_code = 500
resp.reason = "Internal Server Error"
resp.request = requests.Request("POST", "https://databricks.com/api/2.0/service").prepare()
resp.request.body = io.BytesIO(b"binary data")
resp._content = b"unparseable response"

parser = errors._Parser()
error = parser.get_api_error(resp)

# Should not crash and should show [raw stream] for the request body
error_message = str(error)
assert "[raw stream]" in error_message
assert "unparseable response" in error_message