[PR #7618] fix(sdk): improve JSON parsing error handling with contextual messages #12456

Open
opened 2026-02-16 18:17:21 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7618

State: open
Merged: No


Summary

When API responses return empty or malformed JSON (e.g., network interruption, API timeout, truncated response), the SDK now provides actionable error messages instead of cryptic SyntaxError: Unexpected end of JSON input.

Problem

The current implementation calls response.json() directly without error handling:

case "json":
  data = await response[parseAs]()  // Throws unhelpful error on malformed JSON
  break

When the API returns an incomplete response, users see:

SyntaxError: Unexpected end of JSON input
    at json (unknown)
    at <anonymous> (../sdk/js/src/v2/gen/client/client.gen.ts:167:33)

This provides no context about what went wrong or how to debug it.

Solution

Wrap response.json() in try-catch and provide enhanced error messages:

  • Include HTTP status code
  • Include request URL
  • Preserve original error as cause for debugging
  • Route errors through interceptors for custom handling

After this fix:

Error: Failed to parse JSON response: Unexpected end of JSON input. Status: 200, URL: https://api.example.com/v1/chat

Changes

  • packages/sdk/js/src/v2/gen/client/client.gen.ts - v2 client
  • packages/sdk/js/src/gen/client/client.gen.ts - v1 client

Note

These files are auto-generated by @hey-api/openapi-ts. This is a temporary fix until upstream handles JSON parsing errors gracefully. The fix will need to be reapplied after regeneration, or ideally contributed upstream to @hey-api/client-fetch.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7618 **State:** open **Merged:** No --- ## Summary When API responses return empty or malformed JSON (e.g., network interruption, API timeout, truncated response), the SDK now provides actionable error messages instead of cryptic `SyntaxError: Unexpected end of JSON input`. ## Problem The current implementation calls `response.json()` directly without error handling: ```typescript case "json": data = await response[parseAs]() // Throws unhelpful error on malformed JSON break ``` When the API returns an incomplete response, users see: ``` SyntaxError: Unexpected end of JSON input at json (unknown) at <anonymous> (../sdk/js/src/v2/gen/client/client.gen.ts:167:33) ``` This provides no context about what went wrong or how to debug it. ## Solution Wrap `response.json()` in try-catch and provide enhanced error messages: - Include HTTP status code - Include request URL - Preserve original error as `cause` for debugging - Route errors through interceptors for custom handling **After this fix:** ``` Error: Failed to parse JSON response: Unexpected end of JSON input. Status: 200, URL: https://api.example.com/v1/chat ``` ## Changes - `packages/sdk/js/src/v2/gen/client/client.gen.ts` - v2 client - `packages/sdk/js/src/gen/client/client.gen.ts` - v1 client ## Note These files are auto-generated by `@hey-api/openapi-ts`. This is a temporary fix until upstream handles JSON parsing errors gracefully. The fix will need to be reapplied after regeneration, or ideally contributed upstream to `@hey-api/client-fetch`.
yindo added the pull-request label 2026-02-16 18:17:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12456