Free-Way
Free-Way copied to clipboard
[bug] Return invalid_request_error for malformed JSON bodies
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/completionsPOST /v1/messagesPOST /v1/messages/count_tokensPOST /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 buildpasses. - [ ] The PR stays focused on request parsing/error handling.
Hi! I’d like to work on this issue :))
Could you please assign it to me?
Thanks!
Assigned to you @Flames4fun! Looking forward to your PR.