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

Error event fired twice when in development mode #12093

Closed
marcellomontemagno opened this issue Jan 25, 2018 · 3 comments
Closed

Error event fired twice when in development mode #12093

marcellomontemagno opened this issue Jan 25, 2018 · 3 comments

Comments

@marcellomontemagno
Copy link

marcellomontemagno commented Jan 25, 2018

Do you want to request a feature or report a bug?

bug

What is the current behavior?

Error event fired twice when in development mode.

In the example you can see that '>>>>>>error' is logged twice while I only expect to see it once.

Note: this issue doesn't happen with the production version of react

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

static html reproducing the problem:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">

      function handleErr(event){
        console.log('>>>>>>error')
      }

      window.addEventListener('error', handleErr);

      function onClick (e) {
        e.stopPropagation()
        e.preventDefault()
        throw new Error('-------------');
      }

      ReactDOM.render(
        <button onClick={onClick}>button</button>,
        document.getElementById('root')
      );

    </script>
  </body>
</html>

What is the expected behavior?

I expect to see '>>>>>>error' logged once

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

reproduced on:

react v16.2.0
Chrome 63.0.3239.132
OSX 10.10.5

@marcellomontemagno marcellomontemagno changed the title error event fired twice when in development mode Error event fired twice when in development mode Jan 25, 2018
@aulisius
Copy link

aulisius commented Jan 26, 2018

I think it has something to do with invokeGuardedCallback. in _DEV_ mode. They dispatch a fake event to preserver dev-tool behaviour. This comment block would give a better idea

// To preserve the expected "Pause on exceptions" behavior, we don't use a
// try-catch in DEV. Instead, we synchronously dispatch a fake event to a fake
// DOM node, and call the user-provided callback from inside an event handler
// for that fake event. If the callback throws, the error is "captured" using
// a global event handler. But because the error happens in a different
// event loop context, it does not interrupt the normal program flow.
// Effectively, this gives us try-catch behavior without actually using
// try-catch. Neat!

This is more like the intended behavior and not a bug.

@marcellomontemagno
Copy link
Author

Hey @aulisius thanks for your reply :)

I understand the intention of preserving the expected "Pause on exceptions" behaviour of the dev tools, I doubt the react developers expect an user defined error event listener to be invoked twice with the same instance of the error though.

I'm not even sure invokeGuardedCallback is the culprit here (the user callbacks are executed in a next message of the event loop but I don't see where the extra throw is happening), I'll try to dig better in the react code asap.

As an extra info I've also noticed that 'error' events are fired twice while 'unhandledrejection' events behave correctly

@gaearon
Copy link
Collaborator

gaearon commented Jan 28, 2018

I think this is a duplicate of #10474.

@gaearon gaearon closed this as completed Jan 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants