Handle malformed JSON in message send API#411
Conversation
Greptile SummaryThis PR guards the
Confidence Score: 4/5Safe to merge — the change is narrow, correctly scoped inside the outer catch, and does not alter any existing code paths. The route change is minimal and correct: the inner catch fires only for parse failures and returns before any business logic executes, so existing paths are untouched. The test file is new and covers only the targeted error case; there are no tests for auth failures, valid payloads, or Zod validation errors, which means regressions in those paths would go undetected by this suite. route.test.ts — the test suite is thin; only the malformed-JSON case is exercised. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant POST as POST /api/messages/send
participant Auth as getAuthContext
participant Body as request.json()
participant Zod as sendMessageSchema.safeParse
participant DB as Supabase
Client->>POST: POST with JSON body
POST->>Auth: getAuthContext(request)
Auth-->>POST: "null (401 Unauthorized) or {user, supabase}"
POST->>Body: await request.json()
Note over Body: NEW: wrapped in try/catch
alt Malformed JSON
Body-->>POST: throws SyntaxError
POST-->>Client: "400 { error: "Invalid JSON body" }"
else Valid JSON
Body-->>POST: parsed object
POST->>Zod: safeParse(body)
alt Validation fails
Zod-->>POST: error
POST-->>Client: "400 { error: "..." }"
else Validation passes
Zod-->>POST: "{recipient, content}"
POST->>DB: look up recipient, find/create conversation, insert message
DB-->>POST: message + conversationId
POST-->>Client: "201 { data: { conversation_id, message } }"
end
end
Reviews (1): Last reviewed commit: "Handle malformed message JSON" | Re-trigger Greptile |
Summary
Closes #410
Testing
Note: the local pre-commit hook could not run through pnpm because pnpm failed before lint with a cache sqlite error (unable to open database file). I ran the scoped Vitest and ESLint commands directly through node_modules instead.