Skip to content

Commit 3e4063a

Browse files
authored
Merge pull request #5 from convox/type-guard-normal
add content type guard catch
2 parents 161cfd5 + 7784738 commit 3e4063a

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/convox/_http.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ def request(
237237
request_id = response.headers.get("request-id")
238238
raise exception_for_response(response.status_code, message, request_id)
239239

240+
# Guard against non-JSON success responses. The Convox console
241+
# returns its HTML UI when no Rack header is present; without
242+
# this check callers would get an opaque JSONDecodeError.
243+
ct = response.headers.get("content-type", "")
244+
if "text/html" in ct:
245+
raise exception_for_response(
246+
502,
247+
"Expected JSON response but received HTML. "
248+
"If you are connecting through a Convox console, "
249+
"make sure the 'rack' parameter is set.",
250+
)
251+
240252
return response
241253

242254
def get(self, path: str, **kwargs: Any) -> httpx.Response:

src/convox/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def from_env(
110110
import os
111111

112112
host, api_key = credentials_from_env()
113-
rack = os.environ.get("CONVOX_RACK")
113+
rack = os.environ.get("CONVOX_RACK") or None
114114
return cls(host, api_key, rack=rack, retry=retry, timeout=timeout)
115115

116116
@classmethod

0 commit comments

Comments
 (0)