Skip to content

Commit b631497

Browse files
committed
MAGE-940 Implement DRY to centralize error handling
1 parent 2165c93 commit b631497

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

Observer/RecommendSettings.php

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,7 @@ public function execute(Observer $observer)
9898
*/
9999
protected function validateFrequentlyBroughtTogether(string $changedPath): void
100100
{
101-
try {
102-
$recommendations = $this->recommendManagement->getBoughtTogetherRecommendation($this->getProductId());
103-
if (empty($recommendations['renderingContent'])) {
104-
throw new LocalizedException(__(
105-
"It appears that there is no trained model available for Algolia application ID %1.",
106-
$this->configHelper->getApplicationID()
107-
));
108-
}
109-
} catch (\Exception $e) {
110-
$this->configWriter->save($changedPath, 0);
111-
throw new LocalizedException(__("Unable to save FBT Recommend configuration due to the following error: " . $e->getMessage()));
112-
}
101+
$this->validateRecommendation($changedPath, 'getBoughtTogetherRecommendation', 'Frequently Bought Together');
113102
}
114103

115104
/**
@@ -119,18 +108,7 @@ protected function validateFrequentlyBroughtTogether(string $changedPath): void
119108
*/
120109
protected function validateRelatedProducts(string $changedPath): void
121110
{
122-
try {
123-
$recommendations = $this->recommendManagement->getRelatedProductsRecommendation($this->getProductId());
124-
if (empty($recommendations['renderingContent'])) {
125-
throw new LocalizedException(__(
126-
"It appears that there is no trained model available for Algolia application ID: %1.",
127-
$this->configHelper->getApplicationID()
128-
));
129-
}
130-
} catch (\Exception $e) {
131-
$this->configWriter->save($changedPath, 0);
132-
throw new LocalizedException(__("Unable to save Related Products Recommend configuration due to the following error: ". $e->getMessage()));
133-
}
111+
$this->validateRecommendation($changedPath, 'getRelatedProductsRecommendation', 'Related Products');
134112
}
135113

136114
/**
@@ -140,19 +118,7 @@ protected function validateRelatedProducts(string $changedPath): void
140118
*/
141119
protected function validateTrendingItems(string $changedPath): void
142120
{
143-
try {
144-
$recommendations = $this->recommendManagement->getTrendingItemsRecommendation();
145-
// When no recommendations suggested, most likely trained model is missing
146-
if (empty($recommendations['renderingContent'])) {
147-
throw new LocalizedException(__(
148-
"It appears that there is no trained model available for Algolia application ID: %1.",
149-
$this->configHelper->getApplicationID()
150-
));
151-
}
152-
} catch (\Exception $e) {
153-
$this->configWriter->save($changedPath, 0);
154-
throw new LocalizedException(__("Unable to save Trending Items Recommend configuration due to the following error: ". $e->getMessage()));
155-
}
121+
$this->validateRecommendation($changedPath, 'getTrendingItemsRecommendation', 'Trending Items');
156122
}
157123

158124
/**
@@ -161,18 +127,35 @@ protected function validateTrendingItems(string $changedPath): void
161127
* @throws LocalizedException
162128
*/
163129
protected function validateLookingSimilar(string $changedPath): void
130+
{
131+
$this->validateRecommendation($changedPath, 'getLookingSimilarRecommendation', 'Looking Similar');
132+
}
133+
134+
/**
135+
* @param string $changedPath - config path to be reverted if validation failed
136+
* @param string $recommendationMethod - name of method to call to retrieve method from RecommendManagementInterface
137+
* @param string $modelName - user friendly name to refer to model in error messaging
138+
* @return void
139+
* @throws LocalizedException
140+
*/
141+
protected function validateRecommendation(string $changedPath, string $recommendationMethod, string $modelName): void
164142
{
165143
try {
166-
$recommendations = $this->recommendManagement->getLookingSimilarRecommendation($this->getProductId());
144+
$recommendations = $this->recommendManagement->$recommendationMethod($this->getProductId());
167145
if (empty($recommendations['renderingContent'])) {
168146
throw new LocalizedException(__(
169-
"It appears that there is no trained model available for Algolia application ID: %1.",
147+
"It appears that there is no trained model available for Algolia application ID %1.",
170148
$this->configHelper->getApplicationID()
171149
));
172150
}
173151
} catch (\Exception $e) {
174152
$this->configWriter->save($changedPath, 0);
175-
throw new LocalizedException(__("Unable to save Looking Similar Recommend configuration due to the following error: ". $e->getMessage()));
153+
throw new LocalizedException(__(
154+
"Unable to save %1 Recommend configuration due to the following error: %2",
155+
$modelName,
156+
$e->getMessage()
157+
)
158+
);
176159
}
177160
}
178161

0 commit comments

Comments
 (0)