Skip to content

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

@perry-the-pr-reviewer

Description

@perry-the-pr-reviewer

Bug

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

// 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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions