opencode icon indicating copy to clipboard operation
opencode copied to clipboard

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

Open 1186258278 opened this issue 10 months ago • 7 comments

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

---

1186258278 avatar Jan 14 '26 13:01 1186258278

This issue might be a duplicate of existing issues. Please check:

  • #7889: SDK throws 'Unexpected end of JSON input' on empty response bodies (exact duplicate with same root cause in client.gen.ts:167)
  • #7715: Unexpected end of json (related duplicate, marked as bug)
  • #8014: Error: Unexpected end of JSON input (same error message, Windows platform)
  • #8131: SyntaxError: Unexpected token 'at json' in client.gen.ts:167 (exact same location)
  • #7946: Crash on double-escape abort: JSON parse error (same error at client.gen.ts:167)
  • #7985: UI overlay breaks when Vertex AI returns truncated JSON responses (same error, suggests transient JSON parse errors need handling)

Feel free to ignore if none of these address your specific case.

github-actions[bot] avatar Jan 14 '26 13:01 github-actions[bot]

Thanks for the duplicate detection!

This is indeed the same issue as #7889 (root cause: client.gen.ts:167).

Closing in favor of tracking #7889.

1186258278 avatar Jan 14 '26 13:01 1186258278

→ opencode { "name": "UnknownError", "data": { "message": "TypeError: undefined is not an object (evaluating 'input.id')\n at gitlab (src/provider/provider.ts:398:35)\n at gitlab (src/provider/provider.ts:395:18)\n at (src/provider/provider.ts:868:28)\n at processTicksAndRejections (native:7:39)" } }

Y-ASLant avatar Jan 14 '26 13:01 Y-ASLant

@Y-ASLant what caused that to trigger?

rekram1-node avatar Jan 14 '26 13:01 rekram1-node

是什么触发了这个?

我今天想手动升级版本,就执行了npm install -g opencode-ai ,接着就出现了这个问题,然后我卸载,接着重新安装还是出现这个问题。

Image

Y-ASLant avatar Jan 14 '26 13:01 Y-ASLant

@Y-ASLant do u have GITLAB_TOKEN set by chance? maybe try insetting that

ill look into what could cause this

rekram1-node avatar Jan 14 '26 13:01 rekram1-node

你有没有碰巧GITLAB_TOKEN设置?也许可以试着把它放进去

我会调查一下可能的原因

没有,也没有使用过gitlab

Image

Y-ASLant avatar Jan 14 '26 14:01 Y-ASLant