Skip to content

fix: include HTTP method and path in plain-object route return error#147

Open
isteelfelix wants to merge 2 commits into
tscircuit:mainfrom
isteelfelix:fix/improve-response-object-error-message
Open

fix: include HTTP method and path in plain-object route return error#147
isteelfelix wants to merge 2 commits into
tscircuit:mainfrom
isteelfelix:fix/improve-response-object-error-message

Conversation

@isteelfelix
Copy link
Copy Markdown

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:

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 })
  • Updated test assertions to match new message format

/claim #30

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
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.method and URL pathname in 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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants