Skip to content

Commit 34bae58

Browse files
johnyrahulclaude
andcommitted
fix: guard whisper_detail error path against non-JSON and non-dict responses
Align error handling with whisper_status pattern: handle empty body, non-JSON responses, and pass status_code as separate exception argument. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 14b3961 commit 34bae58

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/unstract/llmwhisperer/client_v2.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,16 @@ def whisper_detail(self, whisper_hash: str) -> Any:
352352
req = requests.Request("GET", url, headers=self.headers, params=params)
353353
prepared = req.prepare()
354354
response = self._send_request(prepared)
355-
err = json.loads(response.text)
356-
err["status_code"] = response.status_code
355+
if response.status_code != 200:
356+
if not (response.text or "").strip():
357+
raise LLMWhispererClientException("API error: empty response body", response.status_code)
358+
try:
359+
err = json.loads(response.text)
360+
except json.JSONDecodeError as e:
361+
response_preview = response.text[:500] + "..." if len(response.text) > 500 else response.text
362+
raise LLMWhispererClientException(
363+
f"API error: non-JSON response - {response_preview}", response.status_code
364+
) from e
357365
raise LLMWhispererClientException(err, response.status_code)
358366
return json.loads(response.text)
359367

tests/unit/client_v2_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_whisper_detail_not_found(mocker: MockerFixture, client_v2: LLMWhisperer
7575

7676
error = exc_info.value.error_message()
7777
assert error["message"] == "Record not found"
78-
assert error["status_code"] == 400
78+
assert exc_info.value.status_code == 400
7979
mock_send.assert_called_once()
8080

8181

0 commit comments

Comments
 (0)