Skip to content

Commit 01169ba

Browse files
committed
fix: enhance error handling in raise_if_error function to manage non-JSON responses
1 parent 1d93428 commit 01169ba

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

dlt_source_affinity/rest_client.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,23 @@ def get_v1_rest_client(
5757

5858
def raise_if_error(response: Response, *args: Any, **kwargs: Any) -> None:
5959
if response.status_code < 200 or response.status_code >= 300:
60-
error = error_adapter.validate_json(response.text)
61-
response.reason = "\n".join([e.message for e in error.errors])
60+
# Try to parse JSON error response first
61+
try:
62+
error = error_adapter.validate_json(response.text)
63+
response.reason = "\n".join([e.message for e in error.errors])
64+
except Exception as e:
65+
# If JSON parsing fails, assume non-JSON response (likely HTML for 5XX errors)
66+
# For 5XX errors, this enables retry via HTTPError
67+
logger.warning(
68+
f"Failed to parse error response as JSON: {e}. "
69+
f"Status: {response.status_code}, URL: {response.url}, "
70+
f"Response preview: {response.text[:200]}..."
71+
)
72+
if response.status_code >= 500:
73+
response.reason = f"Server error ({response.status_code}): Non-JSON response received (likely HTML)"
74+
else:
75+
response.reason = f"API error ({response.status_code}): Unable to parse error response"
76+
6277
response.raise_for_status()
6378

6479

0 commit comments

Comments
 (0)