Skip to content

fix: return 400 instead of 503 for malformed JSON and invalid Space access - #11

Open
asasemahmed wants to merge 2 commits into
CopilotKit:mainfrom
asasemahmed:fix/workspace-routes-client-errors
Open

asasemahmed wants to merge 2 commits into
CopilotKit:mainfrom
asasemahmed:fix/workspace-routes-client-errors

Conversation

@asasemahmed

Copy link
Copy Markdown

Problem

On the workspace routes (/api/spaces, /api/dots, /api/conversations,
/api/voice/calls), a malformed JSON body returns 503 with "Check the server
configuration and try again." Creating or updating a Dot with a Space that does
not exist also returns 503. Both are caller errors, not server faults, so the
status and message point the user at the wrong thing. The page routes and
/api/tasks already answer malformed JSON with 400.

Change

  • workspaceRoutes onError now returns 400 Invalid JSON request. for a
    SyntaxError.
  • "Space access must include a valid default destination." now returns 400
    instead of 503.
  • Regression test covering both cases.

Testing

typecheck, lint and the full vitest suite pass (157 tests).

…ace routes

Malformed request bodies on /api/spaces, /api/dots, /api/conversations and
/api/voice/calls, and Dot create/update with an unknown Space, fell through to
the generic 503 handler, which tells the caller to check server configuration.
Return 400 with a specific message instead, matching the page routes.

Signed-off-by: asemabdallah <asasem547@gmail.com>

@jerelvelarde jerelvelarde left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend fixing the error-classification regression below before merging. The intended 400 responses for malformed caller JSON are useful, but the global handler also catches JSON parsing failures from upstream services.

Comment thread src/server/workspace-routes.ts Outdated
});
app.all('/copilotkit/*', (c) => platform.handle(c.req.raw));
app.onError((error, c) => {
if (error instanceof SyntaxError)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Scope SyntaxError handling to request-body parsing

This handler catches upstream JSON parsing failures as well as malformed caller JSON. A valid POST to /api/voice/calls/:id/compute reaches VoiceService.compute → Platform.turn → runThreadTurn, where parsing a malformed runtime /info response throws SyntaxError. The new handler then returns 400 Invalid JSON request. even though the caller's JSON is valid; this previously returned 503. That misidentifies a service failure as a client error and can prevent clients from treating it as retryable.

This was confirmed with a focused reproduction against this PR's head: a malformed upstream response produces 400, while removing this handler restores 503. Please catch malformed JSON at the c.req.json() boundary and add a regression case showing that malformed upstream JSON still returns 503. I recommend fixing this before merging.

Catch JSON parse failures where each workspace route reads its request body
instead of mapping every SyntaxError to 400 in the global handler. Malformed
caller JSON still gets a 400 from the route's schema check, while a SyntaxError
raised by an upstream service call stays a 503. Add a regression test for the
upstream case.

Signed-off-by: asemabdallah <asasem547@gmail.com>
@asasemahmed

Copy link
Copy Markdown
Author

Thanks, that makes sense. I moved the handling to the request-body boundary
(c.req.json().catch(() => null), same as app.ts), so only caller JSON is
treated as a 400, and removed the global SyntaxError branch. Added a test where
an upstream call throws SyntaxError and the route still returns 503.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants