Add guidance for invalid route return values#157
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR strengthens response serialization by explicitly rejecting primitive return values from route handlers and adds tests to ensure users get an actionable error message.
Changes:
- Add a runtime guard in response serialization to throw a clearer error when a handler returns a primitive (or
null) directly. - Add parameterized tests covering several invalid primitive return types (e.g.,
undefined,string,number,symbol,bigint).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/errors/do-not-allow-raw-json.test.ts | Adds a table-driven test suite to verify actionable errors for primitive handler returns. |
| src/create-with-winter-spec.ts | Adds a primitive/null guard in serializeResponse with an improved error message. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (rawResponse === null || typeof rawResponse !== "object") { | ||
| const returnedType = rawResponse === null ? "null" : typeof rawResponse | ||
| throw new Error( | ||
| `Use ctx.json({...}) or return a Response instead of returning ${returnedType} directly.` |
| async (req, ctx, next) => { | ||
| try { | ||
| return await next(req, ctx) | ||
| } catch (e: any) { | ||
| return Response.json({ error: e.message }, { status: 500 }) | ||
| } | ||
| }, |
|
Follow-up pushed in 809c6b3 addressing automated review feedback: the primitive-return guidance now says |
Summary
ctx.json({...})or return aResponseinstead of letting low-level response serialization fail.Validation
/claim #30