yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

Fatal error with custom ErrorHandler component #18036

Closed spiritdead closed 4 years ago

spiritdead commented 4 years ago

Using a custom errorHandler defined in a module with a different namespace im getting the error

in my common config i have defined the alias

'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm' => '@vendor/npm-asset',
        '@customNamespace' => '@common/modules/moduleName'
    ],

and in console config i have this

'components' => [
        'errorHandler' => [
            'class' => 'customNamespace\components\ErrorHandler'
        ],
    ],

root@be93cbe95e5f:/app/src# php yii

What steps will reproduce the problem?

What is the expected result?

should display the list of console commands available

What do you get instead?

Fatal error: Uncaught ReflectionException: Class customNamespace\components\ErrorHandler does not exist in /app/vendor/yiisoft/yii2/di/Container.php on line 449

yii\base\InvalidConfigException: Failed to instantiate component or class "customNamespace\components\ErrorHandler". in /app/vendor/yiisoft/yii2/di/Container.php on line 449

Call Stack:
    0.0001     405960   1. {main}() /app/src/yii:0
    0.0137    2377920   2. yii\console\Application->__construct() /app/src/yii:30
    0.0137    2377920   3. yii\console\Application->__construct() /app/vendor/yiisoft/yii2/console/Application.php:89
    0.0138    2381984   4. yii\console\Application->registerErrorHandler() /app/vendor/yiisoft/yii2/base/Application.php:204
    0.0138    2382416   5. yii\console\Application->getErrorHandler() /app/vendor/yiisoft/yii2/base/Application.php:347
    0.0138    2382416   6. yii\console\Application->get() /app/vendor/yiisoft/yii2/console/Application.php:210
    0.0138    2382416   7. yii\console\Application->get() /app/vendor/yiisoft/yii2/base/Module.php:742
    0.0138    2382416   8. yii\BaseYii::createObject() /app/vendor/yiisoft/yii2/di/ServiceLocator.php:137
    0.0138    2382792   9. yii\di\Container->get() /app/vendor/yiisoft/yii2/BaseYii.php:365
    0.0138    2382792  10. yii\di\Container->build() /app/vendor/yiisoft/yii2/di/Container.php:159
    0.0138    2382792  11. yii\di\Container->getDependencies() /app/vendor/yiisoft/yii2/di/Container.php:374

Additional info

This should load aliases before try to load the errorHandler or this will crash due that the alias don't exists

Q A
Yii version 2.0.35
PHP version 7.2
Operating system Ubuntu
rob006 commented 4 years ago

How does '@customNamespace' => '@customNamespace/modules/moduleName' alias supposed to work? It points on itself.

spiritdead commented 4 years ago

How does '@customNamespace' => '@customNamespace/modules/moduleName' alias supposed to work? It points on itself.

Sorry is '@customNamespace' => '@common/modules/moduleName'

i will update it

darkdef commented 4 years ago

@spiritdead Why should this work? Initialization of aliases in the base\Application.php:(204-206) comes after registerErrorHandler:

    $this->registerErrorHandler($config);  <--- Here - register ErrorHandler

    Component::__construct($config);  <--- Here- setting aliases from config
samdark commented 4 years ago

Yeah. Can't use aliases there and we aren't going to change it since it's critical point that error handler is registered first.