Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Allow custom error handling for i18n errors #550

Merged
merged 2 commits into from
Mar 5, 2019

Conversation

lemonmade
Copy link
Member

Right now, when a translation is missing (or in a few other cases), we throw an error from the i18n library and are done with it. IMO this is the proper default; React provides ways to catch this error in the tree and recover gracefully. However, Web has been bitten a few times by this where incomplete test coverage/ being a little careless with the translations + no additional error handling means that the whole page gets wiped.

This PR adds the necessary hook for us to catch the error. With this PR, if the error is not re-thrown by the onError handler, it will return an empty string instead. IMO this is not really better than the default (since the content will not make sense), and it means that in some cases we still have errors thrown (where we can't return any sensible "default" result), but I seem to be in the minority opinion on this.

Copy link
Member

@GoodForOneFare GoodForOneFare left a comment

Choose a reason for hiding this comment

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

👍

return translate(id, normalizedOptions, this.translations, this.locale);
} catch (error) {
this.onError(error);
return '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make the consumer supply a component for this? Maybe some sort of empty state is better than the empty string? Like a line that looks like it never loads?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this gets really tricky because then you probably want a bunch of context about the type of error and other info. These errors should be rare and dealt with immediately, so I think this is enough to get that done.

Copy link
Contributor

@kyledurand kyledurand left a comment

Choose a reason for hiding this comment

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

❤️

@lemonmade lemonmade merged commit c814f2a into master Mar 5, 2019
@lemonmade lemonmade deleted the allow-custom-i18n-error-handling branch March 5, 2019 21:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants