Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read properties of undefined (reading 'headers') when a route handler doesn't return a response #51130

Closed
1 task done
joulev opened this issue Jun 11, 2023 · 1 comment · Fixed by #51394
Closed
1 task done
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked

Comments

@joulev
Copy link
Contributor

joulev commented Jun 11, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:43 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T8112
    Binaries:
      Node: 20.0.0
      npm: 9.6.4
      Yarn: 1.22.19
      pnpm: 8.6.1
    Relevant packages:
      next: 13.4.5
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/joulev/debug/tree/nextjs-route-handler-not-return-error-bug

To Reproduce

Add a route handler that doesn't return a response. For example

export function POST() {}

Run that route handler. For example in the reproduction repository, make a POST request to /api.

Describe the Bug

The following error is thrown:

- error TypeError: Cannot read properties of undefined (reading 'headers')
    at /Users/joulev/dev/www/debug/.next/server/chunks/322.js:3106:61
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

It is very unclear and has confused many people in the Next.js Discord server. Of course the error can be fixed by returning a response, but the error is too unclear for developers to know what's wrong.

Expected Behavior

I think it would be good if this either returns a default 200 response (new Response(null) for example) (I prefer this), or throws a clearer error, or both.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@joulev joulev added the bug Issue was opened via the bug report template. label Jun 11, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label Jun 11, 2023
@kodiakhq kodiakhq bot closed this as completed in #51394 Sep 8, 2023
kodiakhq bot pushed a commit that referenced this issue Sep 8, 2023
… TS plugin) and at runtime (#51394)

### What?

Fixes #51130. Before this PR, the package assumes that route handlers return a `Response` which is not necessarily the case.

The linked issue specified three suggestions to resolve this

1. Return a default 200 response
2. Throw a better error message
3. or both

~~In this issue I implemented (3), except that it is a warning and not an error. Do tell if the team wants to follow a different approach, as it is not too hard to change this.~~

This PR implements (2).

### How?

The returned value of the handler is checked at runtime to ensure it is actually a `Response` instance.

The return type `AppRouteHandlerFn` is also modified to `unknown` to avoid similar assumptions elsewhere.

The TS plugin is also modified to check for the return type during build time.



Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked
Projects
None yet
1 participant