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

Feature request: Warnings hook #10072

Closed
dtinth opened this issue Jun 29, 2017 · 4 comments
Closed

Feature request: Warnings hook #10072

dtinth opened this issue Jun 29, 2017 · 4 comments

Comments

@dtinth
Copy link

dtinth commented Jun 29, 2017

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

What is the current behavior?

  • Warning code is hardcoded to call fbjs/lib/warning. This makes it hard to integrate warnings with tools.

  • As a workaround we could use webpack’s resolve.alias to alias fbjs/lib/warning into our fork which displays it on-screen.

  • Another use case is to integrate React warnings with our testing infrastructure. This helps us to better see which warning belongs to which test.

    image

What is the expected behavior?

  • React allows library user to override the default warnings behavior.

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

  • The workaround works in React 15 and should still work in React 16, as the flat bundles does var warning = require('fbjs/lib/warning') which means we can still hook into it for now.

  • This probably breaks when the flat bundles stopped doing var warning = require('fbjs/lib/warning').

@gaearon
Copy link
Collaborator

gaearon commented Jul 11, 2017

For now, the easiest workaround is to override console.error and console.warn. But yes, ideally we’d have an API for this. It’s not super clear what it would look like. We might look at this during 16.x release cycle.

@dtinth
Copy link
Author

dtinth commented Jul 11, 2017

@gaearon We’ve considered that approach, but it breaks devtools traceback.

For example, given a file log.js:

// log.js
console.error('Warning 1')
console.error('Warning 2')

When run, devtools will show the source location where console.error was called.

image

By overriding console.error like this:

// override.js
console.error = (original => function () {
  return original.apply(this, arguments)
})(console.error)

Now all console.error calls will be seen as being made from override.js always, even if all we do is passing through all errors.

image

@gaearon
Copy link
Collaborator

gaearon commented Jul 11, 2017

Good observation.

@gaearon
Copy link
Collaborator

gaearon commented Oct 4, 2017

Going to close as a duplicate of #4302.

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