Skip to content

Commit 115468d

Browse files
fern-supportclaude
andcommitted
fix: Guard _event_hook with descriptive error handling
Wrap JSON parse, request transform, and OCI signing in try/except with RuntimeError that names the endpoint and original error, instead of letting exceptions propagate as opaque httpx hook errors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0d26484 commit 115468d

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/cohere/oci_client.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ def _event_hook(request: httpx.Request) -> None:
295295
# Extract Cohere API details
296296
path_parts = request.url.path.split("/")
297297
endpoint = path_parts[-1]
298-
body = json.loads(request.read())
298+
try:
299+
body = json.loads(request.read())
300+
except json.JSONDecodeError as e:
301+
raise RuntimeError(f"OCI client: failed to parse request body as JSON: {e}") from e
299302

300303
# Build OCI URL
301304
url = get_oci_url(
@@ -305,11 +308,14 @@ def _event_hook(request: httpx.Request) -> None:
305308
)
306309

307310
# Transform request body to OCI format
308-
oci_body = transform_request_to_oci(
309-
endpoint=endpoint,
310-
cohere_body=body,
311-
compartment_id=oci_compartment_id,
312-
)
311+
try:
312+
oci_body = transform_request_to_oci(
313+
endpoint=endpoint,
314+
cohere_body=body,
315+
compartment_id=oci_compartment_id,
316+
)
317+
except (KeyError, ValueError) as e:
318+
raise RuntimeError(f"OCI client: failed to transform request for endpoint '{endpoint}': {e}") from e
313319

314320
# Prepare request for signing
315321
oci_body_bytes = json.dumps(oci_body).encode("utf-8")
@@ -330,7 +336,10 @@ def _event_hook(request: httpx.Request) -> None:
330336
prepped_request = oci_request.prepare()
331337

332338
# Sign the request using OCI signer (modifies headers in place)
333-
signer.do_request_sign(prepped_request)
339+
try:
340+
signer.do_request_sign(prepped_request)
341+
except Exception as e:
342+
raise RuntimeError(f"OCI client: request signing failed for endpoint '{endpoint}': {e}") from e
334343

335344
# Update httpx request with signed headers
336345
request.url = URL(url)

0 commit comments

Comments
 (0)