Skip to content

Return 400/404 instead of 500 for bad requests to the local API #2

Open

Description

@blixvip

The server's catch-all maps any error without a status to HTTP 500 and prints a stack trace to the console. Several ordinary user mistakes take that path. Each of these was run against a fresh server:

Request Got Should be
GET /api/projects/nope 500 ENOENT: no such file or directory, open '…/projects/nope.json' 404
DELETE /api/library/nope 500 ENOENT …/images/nope.json 404
POST /api/direct with {"prompt":""} 500 Write an idea first 400
POST /api/direct with {"director":"bogus"} 500 Unknown director bogus 400
POST /api/upload with header x-file-name: 100%.png 500 URI malformed 400, or fall back to a default name

The reverse problem also exists. When a director model returns broken JSON, parseJson() re-throws the SyntaxError from its second JSON.parse. The handler maps every SyntaxError to 400, so the model's fault comes back as a client error with a message like "Unexpected token…".

Task:

  • give these errors a status (the codebase already uses Object.assign(new Error(...), { status }))
  • map ENOENT from loadProject and deleteImage to 404
  • make decodeURIComponent of x-file-name safe
  • wrap the second JSON.parse in parseJson so it throws "The director did not return JSON"
  • add steps to scripts/check.mjs for the 404 and 400 cases

Where: server/index.mjs:116, server/index.mjs:118, server/index.mjs:138-141, server/index.mjs:146-150; server/library.mjs:49-54 (deleteImage), server/library.mjs:66-68 (loadProject); server/director.mjs:60, server/director.mjs:67-68, server/director.mjs:78; scripts/check.mjs.

Done when: npm run check passes with new steps asserting 404 for GET /api/projects/nope and DELETE /api/library/nope, 400 for POST /api/direct with {"prompt":""} and with {"director":"bogus"}, and a non-500 answer for POST /api/upload with x-file-name: 100%.png, with no stack trace in the server console; a director reply that isn't JSON surfaces as "The director did not return JSON", not a 400 Unexpected token….


Getting started: Node 22, no npm install needed: git clone, then npm test and npm run check. Comment here to claim it. Questions → Discord.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions