fix sse error handling if shop api error part 4

This commit is contained in:
team2
2026-04-25 21:13:37 +02:00
parent d13e5537ed
commit 23c7475150
2 changed files with 97 additions and 6 deletions

View File

@@ -393,8 +393,22 @@ final class ShopSearchService
); );
if ($minimalResults !== null) { if ($minimalResults !== null) {
$this->resetLastSearchFailure();
return $minimalResults; return $minimalResults;
} }
$ultraSafeResults = $this->retryWithUltraSafeCriteria(
query: $query,
commerceIntent: $commerceIntent,
originalPrompt: $originalPrompt,
usesHistoryContext: $usesHistoryContext,
previousException: $e
);
if ($ultraSafeResults !== null) {
$this->resetLastSearchFailure();
return $ultraSafeResults;
}
} }
$this->recordFailedSearch($e); $this->recordFailedSearch($e);
@@ -455,7 +469,6 @@ final class ShopSearchService
TransportExceptionInterface | TransportExceptionInterface |
\RuntimeException $safeException \RuntimeException $safeException
) { ) {
$this->recordFailedSearch($safeException);
$this->logger->warning('Shop search safe criteria retry failed', [ $this->logger->warning('Shop search safe criteria retry failed', [
'commerceIntent' => $commerceIntent, 'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt, 'originalPrompt' => $originalPrompt,
@@ -510,7 +523,6 @@ final class ShopSearchService
TransportExceptionInterface | TransportExceptionInterface |
\RuntimeException $minimalException \RuntimeException $minimalException
) { ) {
$this->recordFailedSearch($minimalException);
$this->logger->warning('Shop search minimal UTF-8-safe criteria retry failed', [ $this->logger->warning('Shop search minimal UTF-8-safe criteria retry failed', [
'commerceIntent' => $commerceIntent, 'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt, 'originalPrompt' => $originalPrompt,
@@ -525,6 +537,59 @@ final class ShopSearchService
} }
} }
/**
* @return ShopProductResult[]|null
*/
private function retryWithUltraSafeCriteria(
CommerceSearchQuery $query,
string $commerceIntent,
string $originalPrompt,
bool $usesHistoryContext,
StoreApiException $previousException
): ?array {
$this->logger->warning('Shop search retrying with ultra-safe UTF-8 criteria', [
'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt,
'normalizedPrompt' => $query->normalizedPrompt,
'searchText' => $query->searchText,
'usesHistoryContext' => $usesHistoryContext,
'previousStatusCode' => $previousException->getStatusCode(),
'previousUtf8Failure' => $previousException->isUtf8Failure(),
'previousExceptionMessage' => $previousException->getMessage(),
]);
try {
$ultraSafeCriteria = $this->criteriaBuilder->buildUltraSafe($query, $this->maxResults);
$response = $this->storeApiClient->searchProducts($ultraSafeCriteria);
return $this->mapAndLogSearchResponse(
response: $response,
query: $query,
commerceIntent: $commerceIntent,
originalPrompt: $originalPrompt,
usesHistoryContext: $usesHistoryContext,
usedSafeCriteria: true
);
} catch (
ClientExceptionInterface |
RedirectionExceptionInterface |
ServerExceptionInterface |
TransportExceptionInterface |
\RuntimeException $ultraSafeException
) {
$this->logger->warning('Shop search ultra-safe UTF-8 criteria retry failed', [
'commerceIntent' => $commerceIntent,
'originalPrompt' => $originalPrompt,
'normalizedPrompt' => $query->normalizedPrompt,
'searchText' => $query->searchText,
'usesHistoryContext' => $usesHistoryContext,
'exceptionClass' => $ultraSafeException::class,
'exceptionMessage' => $ultraSafeException->getMessage(),
]);
return null;
}
}
/** /**
* @param array<mixed> $response * @param array<mixed> $response
* @return ShopProductResult[] * @return ShopProductResult[]
@@ -559,6 +624,32 @@ final class ShopSearchService
return $rankedProducts; return $rankedProducts;
} }
/**
* @param array<mixed> $row
*/
private function extractProductName(array $row): string
{
$translatedName = trim((string) ($row['translated']['name'] ?? ''));
if ($translatedName !== '') {
return $translatedName;
}
$name = trim((string) ($row['name'] ?? ''));
if ($name !== '') {
return $name;
}
$productNumber = trim((string) ($row['productNumber'] ?? ''));
if ($productNumber !== '') {
return 'Produkt ' . $productNumber;
}
return '';
}
private function logShopSearchFailure( private function logShopSearchFailure(
CommerceSearchQuery $query, CommerceSearchQuery $query,
string $commerceIntent, string $commerceIntent,
@@ -959,7 +1050,7 @@ final class ShopSearchService
$results[] = new ShopProductResult( $results[] = new ShopProductResult(
id: (string) ($row['id'] ?? ''), id: (string) ($row['id'] ?? ''),
name: trim((string) ($row['translated']['name'] ?? '')), name: $this->extractProductName($row),
productNumber: isset($row['productNumber']) ? (string) $row['productNumber'] : null, productNumber: isset($row['productNumber']) ? (string) $row['productNumber'] : null,
manufacturer: $this->extractManufacturer($row), manufacturer: $this->extractManufacturer($row),
price: $this->extractPrice($row), price: $this->extractPrice($row),

View File

@@ -11,7 +11,7 @@ final class ShopwareCriteriaBuilder
public function build( public function build(
CommerceSearchQuery $query, CommerceSearchQuery $query,
?int $limit = 25, ?int $limit = 25,
?bool $grouping = true ?bool $grouping = false
): array ): array
{ {
return $this->buildCriteria( return $this->buildCriteria(
@@ -29,7 +29,7 @@ final class ShopwareCriteriaBuilder
public function buildSafe( public function buildSafe(
CommerceSearchQuery $query, CommerceSearchQuery $query,
?int $limit = 25, ?int $limit = 25,
?bool $grouping = true ?bool $grouping = false
): array ): array
{ {
return $this->buildCriteria( return $this->buildCriteria(
@@ -51,7 +51,7 @@ final class ShopwareCriteriaBuilder
public function buildMinimal( public function buildMinimal(
CommerceSearchQuery $query, CommerceSearchQuery $query,
?int $limit = 25, ?int $limit = 25,
?bool $grouping = true ?bool $grouping = false
): array ): array
{ {
return $this->buildCriteria( return $this->buildCriteria(