fix stream error handling
This commit is contained in:
@@ -26,6 +26,7 @@ final readonly class StoreApiClient
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws StoreApiException
|
||||
*/
|
||||
public function searchProducts(array $criteria): array
|
||||
{
|
||||
@@ -43,7 +44,7 @@ final readonly class StoreApiClient
|
||||
|
||||
$response = $this->httpClient->request('POST', $url, [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Accept' => 'application/json',
|
||||
'sw-access-key' => $this->salesChannelAccessKey,
|
||||
],
|
||||
@@ -56,22 +57,54 @@ final readonly class StoreApiClient
|
||||
$content = $this->sanitizeString($content);
|
||||
|
||||
if ($statusCode < 200 || $statusCode >= 300) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Shopware Store API request failed with status %d. Response: %s',
|
||||
$statusCode,
|
||||
mb_substr(trim($content), 0, 1000)
|
||||
));
|
||||
throw $this->buildHttpFailure($statusCode, $content);
|
||||
}
|
||||
|
||||
$data = json_decode($content, true);
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException('Shopware Store API returned invalid JSON.');
|
||||
throw new StoreApiException(
|
||||
'Shopware Store API returned invalid JSON.',
|
||||
$statusCode,
|
||||
true,
|
||||
$this->containsUtf8FailureSignal($content),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function buildHttpFailure(int $statusCode, string $content): StoreApiException
|
||||
{
|
||||
$preview = mb_substr(trim($content), 0, 1000);
|
||||
$utf8Failure = $this->containsUtf8FailureSignal($preview);
|
||||
$serverFailure = $statusCode >= 500;
|
||||
|
||||
return new StoreApiException(
|
||||
sprintf(
|
||||
'Shopware Store API request failed with status %d. Response: %s',
|
||||
$statusCode,
|
||||
$preview
|
||||
),
|
||||
$statusCode,
|
||||
$serverFailure,
|
||||
$utf8Failure,
|
||||
$serverFailure || $utf8Failure
|
||||
);
|
||||
}
|
||||
|
||||
private function containsUtf8FailureSignal(string $content): bool
|
||||
{
|
||||
$normalized = mb_strtolower($content, 'UTF-8');
|
||||
|
||||
return str_contains($normalized, 'malformed utf-8')
|
||||
|| str_contains($normalized, 'malformed utf8')
|
||||
|| str_contains($normalized, 'invalid utf-8')
|
||||
|| str_contains($normalized, 'invalid utf8')
|
||||
|| str_contains($normalized, 'possibly incorrectly encoded');
|
||||
}
|
||||
|
||||
private function sanitizeValue(mixed $value): mixed
|
||||
{
|
||||
if (is_array($value)) {
|
||||
@@ -115,4 +148,4 @@ final readonly class StoreApiClient
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user