0

Custom module takes a dependency on \Magento\Framework\Message\ManagerInterface and calls $this->messageManager->getMessages()->getErrors(); after calling other commands.

When this is called this error is thrown:

[Magento\Framework\Exception\LocalizedException]
  Area code is not set

We are setting the area code on the \Magento\Framework\App\State prior to calling into messageManager.

This didn't happen in magento 2.1.9

Exception trace:
 () at /var/www/html/vendor/magento/framework/App/State.php:154
 Magento\Framework\App\State->getAreaCode() at /var/www/html/vendor/magento/framework/Session/SidResolver.php:94
 Magento\Framework\Session\SidResolver->getSid() at /var/www/html/vendor/magento/framework/Interception/Interceptor.php:58
 Magento\Framework\Session\SidResolver\Interceptor->___callParent() at /var/www/html/vendor/magento/framework/Interception/Interceptor.php:138
 Magento\Framework\Session\SidResolver\Interceptor->Magento\Framework\Interception\{closure}() at /var/www/html/vendor/magento/module-staging/Plugin/Framework/Session/SidResolver.php:53
 Magento\Staging\Plugin\Framework\Session\SidResolver->aroundGetSid() at /var/www/html/vendor/magento/framework/Interception/Interceptor.php:135
 Magento\Framework\Session\SidResolver\Interceptor->Magento\Framework\Interception\{closure}() at /var/www/html/vendor/magento/framework/Interception/Interceptor.php:153
 Magento\Framework\Session\SidResolver\Interceptor->___callPlugins() at /var/www/html/generated/code/Magento/Framework/Session/SidResolver/Interceptor.php:26
 Magento\Framework\Session\SidResolver\Interceptor->getSid() at /var/www/html/generated/code/Magento/Framework/Session/SidResolver/Proxy.php:95
 Magento\Framework\Session\SidResolver\Proxy->getSid() at /var/www/html/vendor/magento/framework/Session/SessionManager.php:186
 Magento\Framework\Session\SessionManager->start() at /var/www/html/generated/code/Magento/Framework/Message/Session/Interceptor.php:24
 Magento\Framework\Message\Session\Interceptor->start() at /var/www/html/vendor/magento/framework/Session/SessionManager.php:130
 Magento\Framework\Session\SessionManager->__construct() at /var/www/html/generated/code/Magento/Framework/Message/Session/Interceptor.php:14
 Magento\Framework\Message\Session\Interceptor->__construct() at /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:111
 Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject() at /var/www/html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:66
 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php:70
 Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/html/generated/code/Magento/Framework/Message/Session/Proxy.php:84
 Magento\Framework\Message\Session\Proxy->_getSubject() at /var/www/html/generated/code/Magento/Framework/Message/Session/Proxy.php:127
 Magento\Framework\Message\Session\Proxy->getData() at /var/www/html/vendor/magento/framework/Message/Manager.php:120
 Magento\Framework\Message\Manager->getMessages() at /var/www/html/vendor/custom/module/Console/Command/SetupCommand.php:139
 Custom\Module\Console\Command\SetupCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:242
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:843
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:193
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/magento/framework/Console/Cli.php:104
 Magento\Framework\Console\Cli->doRun() at /var/www/html/vendor/symfony/console/Application.php:117
 Symfony\Component\Console\Application->run() at /var/www/html/bin/magento:23

I stuck spl_object_hash($this) in /var/www/html/vendor/magento/framework/App/State.php and I've established that the instance of State that I'm calling setAreaCode on is not the same instance that SidResolver is calling getAreaCode on. However I'm not sure if this is just because Magento has serialized the object at some point.

Update: Should also point out that this is happening in Enterprise Edition/Magento Commerce and does not happen in the open source version

  • Do you have your own command? have you tried php bin/magento setup:di:compile? – kunj May 28 '18 at 4:05
  • This is a command in our own module, after installing the module. Have also run setup:di:compile, doesn't change anything – Glenn Slaven May 28 '18 at 4:20
2

Try this:

Add this code to your command file:

/**  
 * @var ObjectManagerFactory  
 */ 
private $objectManagerFactory;

/**  
 * @var ObjectManagerInterface  
 */ 
private $objectManager; 

/**  
 * CreateOrderCommand constructor.  
 * @param ObjectManagerFactory $objectManagerFactory  
 */ 
public function __construct(
    ObjectManagerFactory $objectManagerFactory ) {
    parent::__construct();
    $this->objectManagerFactory = $objectManagerFactory; }

/**  
  * Gets initialized object manager  
  *
  * @return ObjectManagerInterface  
  */ 
protected function getObjectManager() {
    if (null == $this->objectManager) {
        $area = FrontNameResolver::AREA_CODE;

        $this->objectManager = $this->objectManagerFactory->create($_SERVER);

        /** @var \Magento\Framework\App\State $appState */
        $appState = $this->objectManager-get(\Magento\Framework\App\State::class);
        $appState->setAreaCode($area);
        $configLoader = $this->objectManager-get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
        $this->objectManager->configure($configLoader->load($area));
    }
    return $this->objectManager; 
}

Then call this $this->getObjectManager()->get(\{{Module Package}}\{{Module Name}}\{{Your File class path}}::class).

  • So it turns out adding these 2 lines did it $appStateOm = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\State::class); $appStateOm->setAreaCode('adminhtml'); As well as setting setAreaCode on the DI injected appState. Wondering what got you to this idea? – Glenn Slaven May 29 '18 at 0:13
  • Sorry for late reply, yes, you can use only this code but other code loads more configurations. – kunj May 30 '18 at 3:47
0

This happens when the class where you define your command has an injected class which at some point needs a session (eg: customer session).

Try to isolate that class by commenting one by one in your __construct() and use a factory class instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.