Skip to content

Commit 0cef678

Browse files
authored
Fixed fatal error with non existing store
1 parent ab8b8b4 commit 0cef678

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Store\Api\StoreCookieManagerInterface;
1616
use Magento\Framework\App\ObjectManager;
1717
use Magento\Framework\Locale\ResolverInterface;
18+
use Psr\Log\LoggerInterface;
1819

1920
/**
2021
* Process the "Store" header entry
@@ -41,22 +42,30 @@ class StoreProcessor implements HttpHeaderProcessorInterface
4142
*/
4243
private $localeResolver;
4344

45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private $logger;
49+
4450
/**
4551
* @param StoreManagerInterface $storeManager
4652
* @param HttpContext $httpContext
4753
* @param StoreCookieManagerInterface $storeCookieManager
4854
* @param ResolverInterface $localeResolver
55+
* @param LoggerInterface $logger
4956
*/
5057
public function __construct(
5158
StoreManagerInterface $storeManager,
5259
HttpContext $httpContext,
5360
StoreCookieManagerInterface $storeCookieManager,
54-
ResolverInterface $localeResolver = null
61+
ResolverInterface $localeResolver = null,
62+
LoggerInterface $logger = null
5563
) {
5664
$this->storeManager = $storeManager;
5765
$this->httpContext = $httpContext;
5866
$this->storeCookieManager = $storeCookieManager;
5967
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
68+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
6069
}
6170

6271
/**
@@ -71,10 +80,14 @@ public function processHeaderValue(string $headerValue): void
7180
{
7281
if (!empty($headerValue)) {
7382
$storeCode = ltrim(rtrim($headerValue));
74-
if ($this->isStoreValid($storeCode)) {
83+
try {
7584
$this->localeResolver->emulate($this->storeManager->getStore($storeCode)->getId());
85+
// $this->storeManager->getStore($storeCode) throws error with non existing stores
86+
// and logged in the catch
7687
$this->storeManager->setCurrentStore($storeCode);
7788
$this->updateContext($storeCode);
89+
} catch (\Exception $e) {
90+
$this->logger->error($e->getMessage());
7891
}
7992
} elseif (!$this->isAlreadySet()) {
8093
$storeCode = $this->storeCookieManager->getStoreCodeFromCookie()
@@ -110,19 +123,4 @@ private function isAlreadySet(): bool
110123

111124
return $this->httpContext->getValue($storeKey) !== null;
112125
}
113-
114-
/**
115-
* Check if provided store code exist
116-
*
117-
* @param string $storeCode
118-
* @return bool
119-
*/
120-
private function isStoreValid(string $storeCode): bool
121-
{
122-
$stores = $this->storeManager->getStores(true, true);
123-
if (isset($stores[$storeCode])) {
124-
return true;
125-
}
126-
return false;
127-
}
128126
}

0 commit comments

Comments
 (0)