Skip to content

Commit 1132aac

Browse files
committed
fix: read declared error fields from the response bytes
1 parent 292090c commit 1132aac

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎_test_unstructured_client/unit/test_declared_errors.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23

34
import httpx
45
import pytest
@@ -84,15 +85,31 @@ def test_parse_declared_error_fields(text, expected):
8485
],
8586
ids=["ServerError", "SDKError", "HTTPValidationError"],
8687
)
87-
def test_error_classes_expose_declared_fields(build):
88-
assert _declared(build(_response(503, json=DECLARED_BODY))) == DECLARED_FIELDS
88+
@pytest.mark.parametrize(
89+
"content",
90+
[
91+
json.dumps(DECLARED_BODY).encode(),
92+
b"\xef\xbb\xbf" + json.dumps(DECLARED_BODY).encode(),
93+
json.dumps(DECLARED_BODY).encode("utf-16"),
94+
],
95+
ids=["utf-8", "utf-8-bom", "utf-16"],
96+
)
97+
def test_error_classes_expose_declared_fields(build, content):
98+
response = _response(503, content=content, headers={"Content-Type": "application/json"})
99+
100+
assert _declared(build(response)) == DECLARED_FIELDS
101+
89102

103+
def test_unread_stream_falls_back_to_body():
104+
class Stream(httpx.SyncByteStream):
105+
def __iter__(self):
106+
pytest.fail("error construction read an unread stream")
107+
yield b""
90108

91-
def test_explicit_body_takes_precedence_over_response_text():
92-
response = _response(500, json={"detail": "x"})
93-
error = SDKError("API error occurred", response, '{"retryable":false}')
109+
response = _response(503, stream=Stream())
110+
error = SDKError("API error occurred", response, json.dumps(DECLARED_BODY))
94111

95-
assert error.retryable is False
112+
assert _declared(error) == DECLARED_FIELDS
96113

97114

98115
def test_success_response_declares_nothing():

‎src/unstructured_client/models/errors/unstructuredclienterror.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
22

33
import httpx
4-
from typing import Optional
4+
from typing import Optional, Union
55

66
from unstructured_client.utils.declared_errors import (
77
DeclaredErrorFields,
@@ -34,7 +34,7 @@ def __init__(
3434
# A success response that failed validation can carry a large body that
3535
# declares nothing, so only error statuses are parsed.
3636
declared = (
37-
parse_declared_error_fields(self.body)
37+
parse_declared_error_fields(self._raw_body())
3838
if self.status_code >= 400
3939
else DeclaredErrorFields()
4040
)
@@ -43,5 +43,12 @@ def __init__(
4343
self.error_reason = declared.error_reason
4444
self.audience = declared.audience
4545

46+
def _raw_body(self) -> Union[str, bytes]:
47+
# The response bytes keep the JSON encoding detection that decoded text loses.
48+
try:
49+
return self.raw_response.content
50+
except httpx.ResponseNotRead:
51+
return self.body
52+
4653
def __str__(self):
4754
return self.message

0 commit comments

Comments
 (0)