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

IntlExtension > Handle MissingResourceException #3257

Merged
merged 1 commit into from Feb 5, 2020

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Feb 5, 2020

When you want to get the language name of a language you can type:

{{ inputLanguage | language_name }}

But if inputLanguage is not a valid language, a MissingResourceException is thrown, blowing up the whole page.

Since in Twig, there is no way to catch this exception, it becomes extremely hard to work with these helpers if you have no control over the input. There is also no way to first check if the language exists before invoking the filter.

@ruudk ruudk force-pushed the handle-MissingResourceException branch 2 times, most recently from 65fa22c to 14efc95 Compare February 5, 2020 10:10
@fabpot fabpot changed the base branch from 3.x to 2.x February 5, 2020 18:10
@fabpot fabpot force-pushed the handle-MissingResourceException branch from 14efc95 to 2df6814 Compare February 5, 2020 18:10
@fabpot
Copy link
Contributor

fabpot commented Feb 5, 2020

Thank you @ruudk.

fabpot added a commit that referenced this pull request Feb 5, 2020
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead (closes #3257).

Discussion
----------

IntlExtension > Handle MissingResourceException

When you want to get the language name of a language you can type:
```twig
{{ inputLanguage | language_name }}
```

But if `inputLanguage` is not a valid language, a `MissingResourceException` is thrown, blowing up the whole page.

Since in Twig, there is no way to catch this exception, it becomes extremely hard to work with these helpers if you have no control over the input. There is also no way to first check if the language exists before invoking the filter.

Commits
-------

2df6814 IntlExtension > Handle MissingResourceException
@fabpot fabpot merged commit 2df6814 into twigphp:2.x Feb 5, 2020
@ruudk ruudk deleted the handle-MissingResourceException branch February 5, 2020 18:49
@ruudk
Copy link
Contributor Author

ruudk commented Feb 10, 2020

@fabpot Thanks for the fast merge. Would it be possible to tag a new release?

@ruudk
Copy link
Contributor Author

ruudk commented Feb 10, 2020

This should also be merged to the 3.x branch btw.

@fabpot
Copy link
Contributor

fabpot commented Feb 11, 2020

Releases done :)

@ruudk
Copy link
Contributor Author

ruudk commented Feb 11, 2020

@fabpot Thanks, quick question, I just deployed this to production and got some errors:

{{ inputCountry | country_name('en') }}

When inputCountry is null it will give this error:

Argument 1 passed to Twig\Extra\Intl\IntlExtension::getCountryName() must be of the type string, null given

I think we should allow null as input, and return null?

-public function getCountryName(string $country, string $locale = null): string
+public function getCountryName(?string $country, string $locale = null): ?string

And then early return null when the input is null. Would you accept that? Then I will create a PR.

@fabpot
Copy link
Contributor

fabpot commented Feb 13, 2020

I would always return a string, so an empty string when the input is null.

@ruudk
Copy link
Contributor Author

ruudk commented Feb 13, 2020

@fabpot Created a PR > #3268

fabpot added a commit that referenced this pull request Feb 13, 2020
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.

Discussion
----------

IntlExtension > Return empty string when input is null

```twig
{{ inputCountry | country_name('en') }}
```
When `inputCountry` is `null` it will give this error:
```
Argument 1 passed to Twig\Extra\Intl\IntlExtension::getCountryName() must be of the type string, null given
```

With this change, an empty string will be returned instead.

See discussion in #3257 (comment)

Commits
-------

caecd2a IntlExtension > Return empty string when input is null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants