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

Throwing inside an async asyncData hook causes headers to be sent twice #278

Closed
Radiergummi opened this issue Feb 23, 2021 · 4 comments
Closed

Comments

@Radiergummi
Copy link

Version

@nuxtjs/sentry: 5.0.2
nuxt: 2.15.1

Sentry configuration

Default config

Reproduction Link

Cannot really reproduce yet

Steps to reproduce

  1. Create a page component with an async asyncData hook that throws an error
    import Vue from 'vue';
     
    export default Vue.extend( {
      async asyncData(): Promise<void> {
        throw new Error('test');
      }
    }
  2. Load the page

What is Expected?

The error is caught, Nuxt's error handler catches it and displays the Youch error page in development or the error.html template in production.

What is actually happening?

A response seems to be sent prematurely, as indicated by the following error reported on the console:

 ERROR  Error: test                                                                                                                                      16:27:30
    at asyncData (src/views/Home.vue:75:0)
    at promisify (dist/utils.js:292:0)
    at server.js:1749:82
    at Array.map (<anonymous>)
    at module.exports../dist/server.js.__webpack_exports__.default (dist/server.js:269:0)


 ERROR  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client                                                              16:27:30
    at new NodeError (node:internal/errors:329:5)
    at ServerResponse.setHeader (node:_http_outgoing:573:11)
    at sendResponse (/node_modules/@nuxt/server/dist/server.js:312:9)
    at errorMiddleware (/node_modules/@nuxt/server/dist/server.js:371:3)

Therefore, the default node HTTP handler error output is sent to the browser (I cannot determine which is the first response generated, presumably the one by Node itself):

HTTP/1.1 500 Internal Server Error
Connection: keep-alive
Content-Length: 523
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Date: Tue, 23 Feb 2021 12:44:54 GMT
Keep-Alive: timeout=5
X-Content-Type-Options: nosniff

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: test<br>
    at asyncData (src/views/Home.vue:75:0)<br>
    at promisify (dist/utils.js:292:0)<br>
    at server.js:1749:82<br>
    at Array.map (<anonymous>)<br>
    at module.exports../dist/server.js.__webpack_exports__.default (dist/server.js:269:0)</pre>
</body>
</html>

Could this be related to #245? I noticed the error only appears if the sentry module is enabled in the nuxt configuration.

Copy link
Member

rchl commented Feb 23, 2021

The output you are getting (a simple HTML page with a pre tag) seems oddly specific. Like something, you or some package would add itself. I don't think Sentry does anything like that (at least when using default settings) and Nuxt's error also would have a different output.

I can't help more without having a look at repo that reproduces.

@rchl rchl added question and removed bug labels Feb 23, 2021
@Radiergummi
Copy link
Author

Radiergummi commented Feb 24, 2021

@rchl this HTML output is specifically what node's http module generates for some errors. Sentry does (or shouldn't) do this, and it's likely caused by the error not being catched, so it bubbles up to the Node.js error handler, which starts emitting a response, then the Nuxt error middleware starts emitting headers again (here). As node has finished writing the headers to the socket already, it can't emit them again, and panicks.
So the problem likely is async errors not being catched by the Sentry error handler and bypassing Nuxt's error handling entirely.
I'll try to create a repro.

@rchl
Copy link
Member

rchl commented Mar 26, 2021

I can't reproduce that. I'm getting a normal Nuxt error and the error is also reported to Sentry correctly.

Screenshot 2021-03-26 at 11 13 46

Maybe try adding config: { debug: true } to module configuration and check if you see something relevant.

I can only think of this happening when there is some failure to report the error to Sentry that would make the app crash before reaching the normal error handler.

@Radiergummi
Copy link
Author

hey @rchl I didn't get around to looking into this, so I'm going to close this issue for now. Still, thank you for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants