fix: include HTTP method and path in plain-object route return error#147
Open
isteelfelix wants to merge 2 commits into
Open
fix: include HTTP method and path in plain-object route return error#147isteelfelix wants to merge 2 commits into
isteelfelix wants to merge 2 commits into
Conversation
When a route handler returns a plain object instead of a Response,
include the HTTP method and path in the error message so developers
can immediately identify which handler needs fixing.
Before:
Use ctx.json({...}) instead of returning an object directly.
After:
Route handler for GET /api/users returned a plain object instead of a Response.
Use ctx.json({...}) instead of returning an object directly.
Example fix: return ctx.json({ ok: true })
Closes tscircuit#30
There was a problem hiding this comment.
Pull request overview
Improves the developer-facing error produced when a route handler mistakenly returns a plain object rather than a Response/ctx.json(), by including the HTTP method and route path in the error message.
Changes:
- Enhance the raw-object response check to include
req.methodand URLpathnamein the thrown error message. - Update the existing raw-JSON error test to match the new message format.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/middleware/with-response-object-check.ts |
Expands the thrown error message to include HTTP method and URL pathname. |
tests/errors/do-not-allow-raw-json.test.ts |
Adjusts assertions to accommodate the updated error message content. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (typeof rawResponse === "object" && !(rawResponse instanceof Response)) { | ||
| const method = req.method | ||
| const path = new URL(req.url).pathname | ||
| throw new Error( |
| "Use ctx.json({...}) instead of returning an object directly" | ||
| ) | ||
| ) | ||
| t.true(data.error.includes("GET")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a route handler accidentally returns a plain object instead of
ctx.json(), the error message now includes the HTTP method and path to immediately pinpoint the problem.Before:
After:
/claim #30