Skip to content

Commit 1453616

Browse files
committed
MAGE-1201 Remove hard fail on Recommend API test exceptions
1 parent bbe1e57 commit 1453616

File tree

1 file changed

+86
-19
lines changed

1 file changed

+86
-19
lines changed

Observer/RecommendSettings.php

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
use Magento\Framework\Event\ObserverInterface;
1515
use Magento\Framework\Exception\LocalizedException;
1616
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
17+
use Psr\Log\LoggerInterface;
1718

1819
class RecommendSettings implements ObserverInterface
1920
{
2021
const QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
2122
const STATUS = 'status';
2223
const VISIBILITY = 'visibility';
2324

25+
const ENFORCE_VALIDATION = 0;
26+
2427
/**
2528
* @var string
2629
*/
@@ -40,7 +43,8 @@ public function __construct(
4043
protected readonly RecommendManagementInterface $recommendManagement,
4144
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder,
4245
protected readonly State $appState,
43-
protected readonly MessageManagerInterface $messageManager
46+
protected readonly MessageManagerInterface $messageManager,
47+
protected readonly LoggerInterface $logger
4448
){}
4549

4650
/**
@@ -147,35 +151,94 @@ protected function validateRecommendation(string $changedPath, string $recommend
147151
{
148152
try {
149153
$recommendations = $this->recommendManagement->$recommendationMethod($this->getProductId());
150-
if ($this->shouldDisplayWarning($recommendations)) {
151-
$this->messageManager->addWarningMessage(__(
152-
"It appears that there is no trained model available for Algolia application ID %1. "
153-
. "Please verify your configuration in the Algolia Dashboard before continuing.",
154-
$this->configHelper->getApplicationID()
155-
));
156-
}
154+
155+
$this->validateRecommendApiResponse($recommendations, $modelName);
157156
} catch (\Exception $e) {
158-
$this->configWriter->save($changedPath, 0);
159-
throw new LocalizedException(__(
160-
"Unable to save %1 Recommend configuration due to the following error: %2",
157+
$this->handleRecommendApiException($e, $modelName, $changedPath);
158+
}
159+
}
160+
161+
/**
162+
* If API does not return a hits response the model may not be configured correctly.
163+
* Do not hard fail but alert the end user.
164+
*
165+
* @throws LocalizedException
166+
*/
167+
protected function validateRecommendApiResponse(array $recommendResponse, string $modelName): void
168+
{
169+
if (!array_key_exists('hits', $recommendResponse)) {
170+
$msg = __(
171+
"It appears that there is no trained %1 model available for Algolia application ID %2. "
172+
. "Please verify your configuration in the Algolia Dashboard before continuing.",
173+
$modelName,
174+
$this->configHelper->getApplicationID()
175+
);
176+
177+
if ($this->shouldDisplayWarning()) {
178+
$this->messageManager->addWarningMessage($msg);
179+
}
180+
else {
181+
$this->logger->warning($msg);
182+
}
183+
}
184+
}
185+
186+
/**
187+
* Handles exceptions on the test request against the Recommend API
188+
*
189+
* The goal is to warn the user of potential issues so they do not enable a model on the front end that has not been
190+
* properly configured in Algolia
191+
*
192+
* TODO: Implement store scoped validation
193+
*
194+
* @throws LocalizedException
195+
*/
196+
protected function handleRecommendApiException(\Exception $e, string $modelName, string $changedPath): void
197+
{
198+
$msg = $this->getUserFriendlyRecommendApiErrorMessage($e);
199+
200+
if (self::ENFORCE_VALIDATION) {
201+
$this->rollBack($changedPath, __(
202+
"Unable to save %1 Recommend configuration due to the following error: %2",
161203
$modelName,
162-
$this->getUserFriendlyRecommendApiErrorMessage($e)
204+
$msg
163205
)
164206
);
165207
}
208+
209+
$msg = __(
210+
"The following error was encountered while enabling %1 recommendations: %2",
211+
$modelName,
212+
$msg
213+
);
214+
215+
if ($this->shouldDisplayWarning()) {
216+
$this->messageManager->addWarningMessage(
217+
$msg
218+
. ' Please verify your configuration in the Algolia Dashboard before continuing.');
219+
}
220+
else {
221+
$this->logger->warning($msg);
222+
}
223+
}
224+
225+
/*
226+
* For hard fail only
227+
*/
228+
protected function rollBack(string $changedPath, \Magento\Framework\Phrase $message): void
229+
{
230+
$this->configWriter->save($changedPath, 0);
231+
throw new LocalizedException($message);
166232
}
167233

168234
/**
169-
* If API does not return a hits response the model may not be configured correctly.
170-
* Do not hard fail but alert the end user.
235+
* Warnings should only be displayed within the admin panel
171236
* @throws LocalizedException
172237
*/
173-
protected function shouldDisplayWarning(array $recommendationResponse): bool
238+
protected function shouldDisplayWarning(): bool
174239
{
175-
return
176-
$this->appState->getAreaCode() === \Magento\Framework\App\Area::AREA_ADMINHTML
177-
&&
178-
!array_key_exists('hits', $recommendationResponse);
240+
return $this->appState->getAreaCode() === \Magento\Framework\App\Area::AREA_ADMINHTML;
241+
179242
}
180243

181244
/**
@@ -200,6 +263,10 @@ protected function getUserFriendlyRecommendApiErrorMessage(\Exception $e): strin
200263
}
201264

202265
/**
266+
* Retrieve a test product for requests against the Recommend API
267+
*
268+
* TODO: Implement store scoping and independently address 404 where objectID is not found
269+
*
203270
* @return string - Product ID string for use in API calls
204271
* @throws LocalizedException
205272
*/

0 commit comments

Comments
 (0)