client.gen.ts: Improve error handling for empty/invalid JSON responses #5768

Closed
opened 2026-02-16 17:56:34 -05:00 by yindo · 0 comments
Owner

Originally created by @1186258278 on GitHub (Jan 14, 2026).

Originally assigned to: @rekram1-node on GitHub.

Bug Report

When the API returns a 200 OK response with an empty body (not 204), response.json() throws "Unexpected end of JSON input".

Error

Error: Unexpected end of JSON input
at json (unknown)
at client.gen.ts:167:33

Current Code (line ~167)

case "json":
  data = await response[parseAs]()  // No try-catch
  break

Problem

The code handles 204 responses correctly (line 133), but when server returns:
- 200 OK with empty body
- 200 OK with invalid JSON
- Network-truncated JSON

The response.json() throws without meaningful error context.

Suggested Fix

case "json":
  try {
    data = await response[parseAs]()
  } catch (err) {
    if (response.status === 200 && !response.body) {
      data = null
    } else {
      throw new Error(`Failed to parse JSON: ${err.message}`)
    }
  }
  break

Environment

- OpenCode: 4347a77d8 (dev)
- OS: Windows
- Node: v22.19.0

---
Originally created by @1186258278 on GitHub (Jan 14, 2026). Originally assigned to: @rekram1-node on GitHub. ## Bug Report When the API returns a 200 OK response with an empty body (not 204), `response.json()` throws "Unexpected end of JSON input". ## Error Error: Unexpected end of JSON input at json (unknown) at client.gen.ts:167:33 ## Current Code (line ~167) ```typescript case "json": data = await response[parseAs]() // No try-catch break Problem The code handles 204 responses correctly (line 133), but when server returns: - 200 OK with empty body - 200 OK with invalid JSON - Network-truncated JSON The response.json() throws without meaningful error context. Suggested Fix case "json": try { data = await response[parseAs]() } catch (err) { if (response.status === 200 && !response.body) { data = null } else { throw new Error(`Failed to parse JSON: ${err.message}`) } } break Environment - OpenCode: 4347a77d8 (dev) - OS: Windows - Node: v22.19.0 ---
yindo added the windows label 2026-02-16 17:56:34 -05:00
yindo closed this issue 2026-02-16 17:56:34 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#5768