Skip to content

Fatal error thrown on exception constructor when cookies store is an array #38365

@digitalpianism

Description

@digitalpianism

Preconditions and environment

  • Magento version 2.4.4-p6

Steps to reproduce

You need to have the $_COOKIE['store'] to be an array and not contain a valid store code.

I don't know exactly how to set it this way but Magento code is written to handle that situation as you can see here:

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Store/Model/StoreResolver.php#L108

After seeing the fatal error popping up in our logs, I had to manually do : $_COOKIE['store'] = ['test']; to replicate the bug.

Expected result

The Invalid store parameter. (https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Store/Model/StoreResolver.php#L110) should be thrown

Actual result

A PHP fatal error is triggered:

PHP message: PHP Fatal error:  Uncaught TypeError: Exception::__construct(): Argument #1 ($message) must be of type string, Magento\\Framework\\Phrase given in /var/www/source/vendor/magento/module-store/Model/StoreResolver.php:110

Additional information

Reason is the PHP InvalidArgumentException does not accept a \Magento\Framework\Phrase but only a string.

Here's the bug fix I used to get rid of the error:

diff --git a/src/magento/module-store/Model/StoreResolver.php b/src/magento/module-store/Model/StoreResolver.php
index 2a950b699a..0da28d2760 100644
--- a/module-store/Model/StoreResolver.php
+++ b/module-store/Model/StoreResolver.php
@@ -107,7 +107,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface

         if (is_array($storeCode)) {
             if (!isset($storeCode['_data']['code'])) {
-                throw new \InvalidArgumentException(__('Invalid store parameter.'));
+                throw new \Magento\Framework\Exception\InvalidArgumentException(__('Invalid store parameter.'));
             }
             $storeCode = $storeCode['_data']['code'];
         }

Note that there's another way of fixing it like this:

diff --git a/src/third-party/magento/module-store/Model/StoreResolver.php b/src/third-party/magento/module-store/Model/StoreResolver.php
index 2a950b699a..0da28d2760 100644
--- a/module-store/Model/StoreResolver.php
+++ b/module-store/Model/StoreResolver.php
@@ -107,7 +107,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface

         if (is_array($storeCode)) {
             if (!isset($storeCode['_data']['code'])) {
-                throw new \InvalidArgumentException(__('Invalid store parameter.'));
+                throw new \InvalidArgumentException('Invalid store parameter.');
             }
             $storeCode = $storeCode['_data']['code'];
         }

I guess it's up to you guys.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

Labels

Area: FrameworkComponent: StoreIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: PR in progressReported on 2.4.4-p6Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions