fix sse error handling if shop api error part 1

This commit is contained in:
team2
2026-04-25 20:16:40 +02:00
parent 93ff6262cc
commit cf970d2b49
3 changed files with 204 additions and 70 deletions

View File

@@ -383,6 +383,18 @@ final class ShopSearchService
if ($safeResults !== null) {
return $safeResults;
}
$minimalResults = $this->retryWithMinimalCriteria(
query: $query,
commerceIntent: $commerceIntent,
originalPrompt: $originalPrompt,
usesHistoryContext: $usesHistoryContext,
previousException: $e
);
if ($minimalResults !== null) {
return $minimalResults;
}
}
$this->recordFailedSearch($e);
@@ -458,6 +470,61 @@ final class ShopSearchService
}
}
/**
* @return ShopProductResult[]|null
*/
private function retryWithMinimalCriteria(
CommerceSearchQuery $query,
string $commerceIntent,
string $originalPrompt,
bool $usesHistoryContext,
StoreApiException $previousException
): ?array {
$this->logger->warning('Shop search retrying with minimal UTF-8-safe criteria', [
'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt,
'normalizedPrompt' => $query->normalizedPrompt,
'searchText' => $query->searchText,
'usesHistoryContext' => $usesHistoryContext,
'previousStatusCode' => $previousException->getStatusCode(),
'previousUtf8Failure' => $previousException->isUtf8Failure(),
'previousExceptionMessage' => $previousException->getMessage(),
]);
try {
$minimalCriteria = $this->criteriaBuilder->buildMinimal($query, $this->maxResults);
$response = $this->storeApiClient->searchProducts($minimalCriteria);
return $this->mapAndLogSearchResponse(
response: $response,
query: $query,
commerceIntent: $commerceIntent,
originalPrompt: $originalPrompt,
usesHistoryContext: $usesHistoryContext,
usedSafeCriteria: true
);
} catch (
ClientExceptionInterface |
RedirectionExceptionInterface |
ServerExceptionInterface |
TransportExceptionInterface |
\RuntimeException $minimalException
) {
$this->recordFailedSearch($minimalException);
$this->logger->warning('Shop search minimal UTF-8-safe criteria retry failed', [
'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt,
'normalizedPrompt' => $query->normalizedPrompt,
'searchText' => $query->searchText,
'usesHistoryContext' => $usesHistoryContext,
'exceptionClass' => $minimalException::class,
'exceptionMessage' => $minimalException->getMessage(),
]);
return null;
}
}
/**
* @param array<mixed> $response
* @return ShopProductResult[]