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

Maybe capture error & warn instead of throw? #109

Closed
longlho opened this issue Apr 30, 2015 · 9 comments
Closed

Maybe capture error & warn instead of throw? #109

longlho opened this issue Apr 30, 2015 · 9 comments

Comments

@longlho
Copy link
Member

longlho commented Apr 30, 2015

Hi @ericf ,

Quick question/suggestion: on this line react-intl actually throws error for invalid input. Is this by design? If yes any chance we can be a bit more forgiving and warn instead of throw? The reason is that this gets bubbled up and croak the server/create unexpected behavior on client and with the React stack trace bloat it's incredibly difficult to trace it.

Thanks!

@ericf
Copy link
Collaborator

ericf commented May 2, 2015

Yes this is intentional, it's an early throw since the underlying Intl.DateTimeFormat() that will be called will throw if the value is not finite.

There's other discussions that have brought up error/warn handling — so we're thinking about it.

@longlho
Copy link
Member Author

longlho commented May 2, 2015

@ericf possible to support a default/fallback prop of some sort? I'm happy to push a PR too if it's cool.

@ericf
Copy link
Collaborator

ericf commented May 2, 2015

I don't understand what you're proposing… can you explain in more detail?

@longlho
Copy link
Member Author

longlho commented May 2, 2015

oh so something like <FormattedDate value={foo} default="N/A"/> which would print out N/A if foo is invalid

@longlho
Copy link
Member Author

longlho commented May 5, 2015

ping @ericf :)

@ericf
Copy link
Collaborator

ericf commented May 6, 2015

What are the actual real-world use cases where you're seeing <FormattedDate> throw? Also, how do you deal with these sorts of exceptions for other data that you try to render in your React components that might be null or something?

@longlho
Copy link
Member Author

longlho commented May 6, 2015

@ericf The real world use case is rather simple: Say I fetch some data from the server that has a field called date, which subsequently gets rendered out via <FormattedDate value={date} />. Now if date is a correct representation of a Date of some sort, everything's happy.

However, if date is undefined, new Date(undefined) per this line will result in an Invalid Date, which throws at this line and on the server side, the process got shut down due to Uncaught Exception. 😢

Currently to overcome this issue we have to wrap FormattedDate inside a component like SafeFormattedDate that does a try catch, and that goes for FormattedTime, FormattedMessage and such...

For other React components, if given an invalid data type it would just render nothing (null/undefined) so since the throw was by design, supporting a default prop in FormattedDate would be a more forgiving opt-in alternative for this behavior.

@ericf ericf removed the needs info label May 27, 2015
@ericf
Copy link
Collaborator

ericf commented May 27, 2015

uses React.PropTypes.any.isRequired so you should be seeing warns from React. My main worry is about silent failures, but I see you're point about rendering null being a reasonable thing to do in this case.

@ericf
Copy link
Collaborator

ericf commented Sep 11, 2015

See: #162

@ericf ericf closed this as completed Sep 11, 2015
longlho pushed a commit that referenced this issue Apr 27, 2020
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