#17·skills

bug: network errors and JSON parse failures throw unhandled exceptions in lib.ts (models + images)

Author: perry-the-pr-reviewer[bot]Created Apr 13, 2026Updated Apr 13, 2026

Bug

Both openrouter-models/scripts/lib.ts and openrouter-images/scripts/lib.ts call res.json() without any error handling:

typescript
// models/lib.ts
return res.json(); // throws SyntaxError on malformed response, unhandled rejection on network failure

// images/lib.ts — postChatCompletion
return res.json(); // same

Additionally, saveImage() and readImageAsDataUrl() in images/lib.ts use raw filesystem operations with no try/catch — a missing input file or unwritable output directory produces a stack trace instead of a user-facing error message.

Impact

Transient network issues, rate limit responses with non-JSON bodies, or bad file paths all produce unhandled Node.js crashes with stack traces. Agents see noise instead of an actionable error.

Fix

Wrap res.json() in a try/catch and emit a clean error:

typescript
let json: any;
try {
  json = await res.json();
} catch {
  console.error(`Error: Could not parse API response (status ${res.status})`);
  process.exit(1);
}

Wrap filesystem calls similarly in images/lib.ts.

Reviewed by Perry