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

checkPropTypes: add argument that allows external logging #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rufman
Copy link

@rufman rufman commented May 17, 2017

When using prop-types for checking things outside of React, having checkPropTypes throw an error instead of swallowing it and logging it to console.error can be useful. The flag throwErrors will throw the error if set to true instead of logging it. AcheckPropTypeWithErrors convenience function, which calls checkPropTypes with throwErrors set to true has also been added.

Fixes #34

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@rufman
Copy link
Author

rufman commented Jun 12, 2017

@gaearon Is this a good approach to using prop-types checking outside of React? Or is there a better way?

@gaearon
Copy link
Contributor

gaearon commented Jun 12, 2017

Sorry, we're busy with many other things right now, and it's unlikely we'll have time to review PRs to prop-types very soon. In this PR, a few things concern me:

  • Creating errors is expensive and we'd like to get away from that, but throwing reinforces this pattern
  • This doesn't expose other important information (such as component stack)

When specified, the argument `warningLogger` will be called with the
same arguments as `warning`. Instead of logging errors to the fbjs
warning logger, we are able to handle them externally.

Fixes facebook#34
@rufman
Copy link
Author

rufman commented Jul 13, 2017

The PR has been updated to not throw an error, but instead calls the function passed in as an argument, with the same arguments as are passed to warning.

@rufman rufman changed the title checkPropTypes: add flag to throw the error instead of logging checkPropTypes: add argument that allows external logging Aug 16, 2017
@rufman
Copy link
Author

rufman commented Sep 5, 2017

I'm aware that there might not be time to review this PR, but with React 16 in beta now the resolution on how to use prop-types with custom errors is becoming a more prominent issue.

I was wondering if there is anything else that would need to be done on this PR to have it merged. Or if such a feature won't be considered for the prop-types package, we'll look into alternatives.

@kaiyoma
Copy link

kaiyoma commented Sep 28, 2017

Any traction here? This is something we're interested in as well.

@martinhorsky
Copy link

@kaiyoma I used check-prop-types module mentioned here: #28 (comment)

ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
ojab added a commit to ojab/redux-orm-proptypes that referenced this pull request Oct 3, 2017
PropTypes.checkPropTypes cannot throw yet (see [1]), so checking
console.error output

[1] facebook/prop-types#54
@JoeDuncko
Copy link

This would also be super useful to me. Any word on whether this might make it in? If it won't, then closing this PR and "officially" referring users to another project that does this might be a good move.

Copy link

@kushthedude kushthedude left a comment

Choose a reason for hiding this comment

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

Remove Conflicts

Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This needs a rebase.

checkPropTypes.js Outdated Show resolved Hide resolved
checkPropTypes.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This looks good, but I'm going to hold off merging it for a bit; while adding an argument is generally semver-minor, if anyone was passing a non-function here, this would suddenly throw (and i think throwing when they pass a truthy non-function is the right behavior)

@ljharb ljharb self-assigned this Sep 4, 2019
@jkusa

This comment has been minimized.

@cythrawll

This comment has been minimized.

@JimmayVV

This comment has been minimized.

@JimmayVV
Copy link

This has been here for quite a while, and have read little to no objections to merging this PR aside from "time" constraints. This would be a wonderful addition to the propTypes library and I am genuinely confused as to what it would take to consider merging it.

Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

I've rebased this, and also changed the warningLogger to default to undefined, so it's not part of the function's length.

The only thing this is missing is similar tests in the other three test files.

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.

Using prop-types outside of React