Free-Way icon indicating copy to clipboard operation
Free-Way copied to clipboard

[bug] Return invalid_request_error for malformed JSON bodies

Open GoDiao opened this issue 7 months ago • 2 comments

Summary

Malformed JSON request bodies should return a 400 invalid request error instead of falling through to a generic 500 internal error.

Background

Several routes call JSON.parse(raw) directly. If parsing fails, the top-level error handler currently turns that into a 500 response. For OpenAI-compatible and Anthropic-compatible clients, invalid JSON should be a client error.

Relevant file:

  • src/server.ts

Routes to consider:

  • POST /v1/chat/completions
  • POST /v1/messages
  • POST /v1/messages/count_tokens
  • POST /api/config/keys

Requirements

  • [ ] Safely parse JSON request bodies.
  • [ ] Return HTTP 400 for malformed JSON.
  • [ ] Use an OpenAI-style error shape where appropriate:
{
  "error": {
    "message": "Invalid JSON body",
    "type": "invalid_request_error"
  }
}

Acceptance Criteria

  • [ ] Malformed JSON no longer returns HTTP 500.
  • [ ] Valid requests continue to work as before.
  • [ ] npm run build passes.
  • [ ] The PR stays focused on request parsing/error handling.

GoDiao avatar May 02 '26 14:05 GoDiao

Hi! I’d like to work on this issue :))

Could you please assign it to me?

Thanks!

Flames4fun avatar May 05 '26 20:05 Flames4fun

Assigned to you @Flames4fun! Looking forward to your PR.

GoDiao avatar May 06 '26 02:05 GoDiao