Skip to content

Commit d67eb7b

Browse files
prevent parsing empty response data (get test_retry_abort_non_recoverable_error to pass)
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent 4651cd6 commit d67eb7b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/databricks/sql/backend/sea/utils/http_client.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,30 @@ def _make_request(
284284

285285
try:
286286
if response.data:
287-
error_details = json.loads(response.data.decode("utf-8"))
288-
if isinstance(error_details, dict) and "message" in error_details:
289-
error_message = f"{error_message}: {error_details['message']}"
290-
logger.error(f"Request failed: {error_details}")
291-
except (json.JSONDecodeError, UnicodeDecodeError):
292-
# Log raw response if we can't parse JSON
293-
content = (
294-
response.data.decode("utf-8", errors="replace") if response.data else ""
295-
)
296-
logger.error(f"Request failed with non-JSON response: {content}")
287+
decoded_data = response.data.decode("utf-8")
288+
# Ensure we have a string before attempting JSON parsing
289+
if isinstance(decoded_data, str):
290+
error_details = json.loads(decoded_data)
291+
if isinstance(error_details, dict) and "message" in error_details:
292+
error_message = f"{error_message}: {error_details['message']}"
293+
logger.error(f"Request failed: {error_details}")
294+
else:
295+
# Handle case where decode returns non-string (e.g., MagicMock in tests)
296+
logger.error(
297+
f"Request failed with non-string response data: {type(decoded_data)}"
298+
)
299+
except (json.JSONDecodeError, UnicodeDecodeError, TypeError):
300+
# Log raw response if we can't parse JSON or if we get unexpected types
301+
try:
302+
content = (
303+
response.data.decode("utf-8", errors="replace")
304+
if response.data
305+
else ""
306+
)
307+
logger.error(f"Request failed with non-JSON response: {content}")
308+
except (AttributeError, TypeError):
309+
# Handle case where response.data itself might be a mock
310+
logger.error(f"Request failed with unparseable response data")
297311

298312
raise RequestError(error_message, None)
299313

0 commit comments

Comments
 (0)