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

Currency formatter InvalidConfigException #6277

Closed
samhibberd opened this issue Jun 27, 2020 · 8 comments
Closed

Currency formatter InvalidConfigException #6277

samhibberd opened this issue Jun 27, 2020 · 8 comments

Comments

@samhibberd
Copy link

samhibberd commented Jun 27, 2020

Description

Running into an odd error (reported in the commerce repo craftcms/commerce#1541) but it think it might be an issue with the core.

Looks to be caused when a string rather than float / number is passed to the currency formatter:

Craft::$app->getFormatter()->asCurrency('26.3')

Throws:

The default currency code for the formatter is not defined.

Error suggests the config exception is thrown from the asCurrencyStringFallback()

Steps to reproduce

  1. Tested by calling the above formatter in a custom module init.
  2. Exception thrown

Additional info

  • Craft version: Craft CMS 3.5.0-beta.3 (599445e)
  • PHP version: 7.3
> yii\base\InvalidConfigException: The default currency code for the formatter is not defined. in /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/i18n/Formatter.php:2076
> Stack trace:
> #0 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/i18n/Formatter.php(1275): yii\i18n\Formatter->asCurrencyStringFallback('26.3', NULL)
> #1 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/src/i18n/Formatter.php(257): yii\i18n\Formatter->asCurrency('26.3', NULL, Array, Array)
> #2 /Users/samhibberd/Sites/beta.findarace.com/modules/findarace/src/Findarace.php(113): craft\i18n\Formatter->asCurrency('26.3')
> #3 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/BaseObject.php(109): modules\findarace\Findarace->init()
> #4 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/Module.php(158): yii\base\BaseObject->__construct(Array)
> #5 /Users/samhibberd/Sites/beta.findarace.com/modules/findarace/src/Findarace.php(95): yii\base\Module->__construct('findarace', Object(craft\web\Application), Array)
> #6 [internal function]: modules\findarace\Findarace->__construct('findarace', Object(craft\web\Application), Array)
> #7 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/di/Container.php(392): ReflectionClass->newInstanceArgs(Array)
> #8 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/di/Container.php(159): yii\di\Container->build('modules\\findara...', Array, Array)
> #9 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('modules\\findara...', Array, Array)
> #10 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/Module.php(427): yii\BaseYii::createObject(Array, Array)
> #11 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/Application.php(315): yii\base\Module->getModule('findarace')
> #12 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/src/web/Application.php(118): yii\base\Application->bootstrap()
> #13 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/Application.php(273): craft\web\Application->bootstrap()
> #14 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/src/web/Application.php(100): yii\base\Application->init()
> #15 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
> #16 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/base/Application.php(206): yii\base\BaseObject->__construct(Array)
> #17 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/src/web/Application.php(90): yii\base\Application->__construct(Array)
> #18 [internal function]: craft\web\Application->__construct(Array)
> #19 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/di/Container.php(400): ReflectionClass->newInstanceArgs(Array)
> #20 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/di/Container.php(159): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
> #21 /Users/samhibberd/Sites/beta.findarace.com/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
> #22 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/bootstrap/bootstrap.php(241): yii\BaseYii::createObject(Array)
> #23 /Users/samhibberd/Sites/beta.findarace.com/vendor/craftcms/cms/bootstrap/web.php(51): require('/Users/samhibbe...')
> #24 /Users/samhibberd/Sites/beta.findarace.com/web/index.php(20): require('/Users/samhibbe...')
> #25 /Users/samhibberd/.composer/vendor/weprovide/valet-plus/server.php(131): require('/Users/samhibbe...')
> #26 {main}
@brandonkelly
Copy link
Member

I’m not able to reproduce this, but appears to be a Yii bug with isNormalizedValueMispresented(). Can be worked around by casting the value to a float, or passing the $currency argument.

@samhibberd
Copy link
Author

samhibberd commented Jul 22, 2020

@brandonkelly this one has cropped up again for me, I did some digging in the Yii slack channel and feedback suggested it could well be a configuration issue.

I have narrowed it down to decimal strings as it only throws the exception if passed a string with decimal '26.4' passing '26' without the decimal and it's all good.

And if I override the configuration (just using the example in the Yii docs) then the issue goes away, https://www.yiiframework.com/doc/guide/2.0/en/output-formatting#configuring-formatter

@brandonkelly
Copy link
Member

The formatter config is provided by the locale, so not something we can tweak.

@samhibberd
Copy link
Author

It doesn't look like the locale config is setting a currencyCode with \Yii::$app->getFormatter()->currencyCode returning null.

Should the locale not be setting that?

@brandonkelly
Copy link
Member

We do in fact set that, if the Intl extension is not installed. Otherwise it’s not needed since the Intl extension will handle it automatically. Just tested and works as expected on my end.

What locale is your site set to? Most locales don’t actually specify a currency. en-GB does, though.

@samhibberd
Copy link
Author

Set to en-GB

@brandonkelly
Copy link
Member

Maybe something is wrong with your Intl extension, then.

@samhibberd
Copy link
Author

It was, finally had a chance to get everything updated locally and all resolved, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants