9

Because of the high reliance of XML files, I am having a very hard time figuring out the cause of this error when creating a custom admin grid:

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception in C:\wamp64\www\mage2\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0

I am trying to make an admin grid for the sales_shipment_item table.

So far I have:

  1. Used xdebug and put it within the __toString() method of the Result class, it does not show what the real error is

  2. I have var_dump-ed the variables within the __toString() method

  3. I have developer mode turned on in Magento 2

  4. I have checked the var/logs and it shows nothing helpful

I have successfully made other admin custom grids, but trying to figure out the actual error is like a needle in a haystack. Has anyone found a better way of debugging these? I have looked over all the grid xml, di.xml, and models and all looks ok.

  • Make a big try/catch block in Magento\Ui\TemplateEngine\Xhtml\Result::__toString(), catch the exception and log it, or print it. It's what M2 should've done anyway. – nevvermind Jun 9 '16 at 20:30
  • yes that is what is already there in Result.php: } catch (\Exception $e) { $this->logger->critical($e->getMessage()); $result = $e->getMessage(); } , the error is what I have already posted. When I have had these errors before it usually has to do with missing di.xml injections but these are impossible to debug without just guessing, which is why I posted this question. – Kevin Chavez Jun 9 '16 at 21:04
  • @KevinJavitz, Did you fix this ? I am having the same problem. – MGento Sep 21 '18 at 6:55
4

The error you're getting is indeed triggered in vendor\magento\module-ui\Component\Wrapper\UiComponent.php.

However, it is not triggered at line 0 but when the result is being converted to a string in the following method in the return statement:

protected function _toHtml()
{
    foreach ($this->getChildNames() as $childName) {
        $childBlock = $this->getLayout()->getBlock($childName);
        if ($childBlock) {
            $wrapper = $this->blockWrapperFactory->create([
                'block' => $childBlock,
                'data' => [
                    'name' => 'block_' . $childName
                ]
            ]);
            $this->component->addComponent('block_' . $childName, $wrapper);
        }
    }

    $result = $this->component->render();
    return (string)$result;
}

Here's what you can try to debug your problem:

  • check what $result contains before the string conversion and return statement is called
  • get informations about the component that is causing an issue by calling $component->getName(), $component->getComponentName() and $component->getData() to help you finding out what the problem is
1

This fatal error also occurred to me when I added custom listing/grid. I fixed this issue by changing constructor and initiating correct collection for my data listing/grid in data provider constructor. Example of custom grid DataProvider.php

use Acme\CustomModule\Model\ResourceModel\Entity\Listing\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, 
        $meta, $data);
        $this->collection = $collectionFactory->create();
    }

    public function getData(): array
    {
        $collection = $this->getCollection();
        return $collection->toArray();
    }
}

But you need to create \Acme\CustomModule\Model\ResourceModel\Entity\Listing\Collection to get it's data in data provider

1

IF You migrated M1 to M2 then you are facing something below error during cmd execution The store that was requested wasn't found. Verify the store and try again.

please don't change in vendor module-store files :

  • /vendor/magento/module-store/Model/StoreManager.php or

    /vendor/magento/module-store/Model/StoreRepository.php

just simply apply below steps:

I just recently came across this same situation after migrating from Magento 1.9.3.8 to 2.3.0 and hope my answer can be of assistance. The problem came from deleting multiple stores 96 of them to be exact. I tried all the other answers here but was still getting the same error.

The fix for me was removing the old store data from inside of the "core_config_data". The problem is that when the Magento is loading the run time configuration data it is finding the old stores and trying to resolve them. Before cleaning the data from the database I HIGHLY recommend you run the SELECT query below to make sure you delete the correct stores.

SELECT * FROM core_config_data WHERE scope = 'stores';

WARNING: MAKE SURE TO BACKUP YOUR DATABASE BEFORE RUNNING THIS!

DELETE FROM core_config_data WHERE scope_id != 1 AND scope = 'stores';

Now execute all magento commands you can see the "The store that was requested wasn't found. Verify the store and try again" fixed by query

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception in C:\wamp64\www\mage2\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0

Now check your admin above fatal error also solved blahhh... blahh...

(Note:- fatal error_Magento\Ui\TemplateEngine\Xhtml\Result::__toString() depedent on store module so don't change /vendor/module-store otherwise admin side grid/list catalog you can'not see proper data)

0

After countless hours and lots of banging my head against the desk, I discovered that I was getting this error, because I was using xdebug (which is amazing!) to breakpoint at a low level function, namely Magento\Ui\TemplateEngine\Xhtml\Result::__toString() in order to test.

Apparently in some way, the output from the debugger was actually throwing an error which caused the __toString method to yell at me.

I was going crazy because the error would only show itself when I had the debugger switched on and thought maybe it had something to do with the AJAX call in populating the UI Listing. Switching it off seemed to make it work, so it's worth giving it a shot. I'm not too sure how the breakpoint functionality of xdebug (in IntelliJ products specifically... possibly) works, other than you are able to invoke a breakpoint with the line xdebug_break(). It's very likely that having a breakpoint in the __toString method is just a silly thing to do in any case.

Guess we can't debug everything dynamically yet... One day!

I really hope this helps someone else.

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.