fix sse error handling if shop api error part 3
This commit is contained in:
@@ -156,7 +156,7 @@ final readonly class AgentRunner
|
||||
|
||||
yield $this->systemMsg(
|
||||
$this->buildShopUnavailableMessage($primaryShopSearchFailureReason),
|
||||
'info'
|
||||
'err'
|
||||
);
|
||||
|
||||
$repairPayload = [
|
||||
@@ -811,12 +811,21 @@ final readonly class AgentRunner
|
||||
|
||||
private function buildUserErrorMessage(Throwable $e): string
|
||||
{
|
||||
$message = trim($e->getMessage());
|
||||
|
||||
if ($message === '') {
|
||||
$message = $e::class;
|
||||
}
|
||||
|
||||
$safeMessage = htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
if (!$this->debug) {
|
||||
return $this->agentRunnerConfig->getGenericInternalErrorMessage();
|
||||
return $this->agentRunnerConfig->getGenericInternalErrorMessage()
|
||||
. '<br><small>Technischer Fehler: ' . $safeMessage . '</small>';
|
||||
}
|
||||
|
||||
return $this->agentRunnerConfig->getDebugInternalErrorPrefix()
|
||||
. htmlspecialchars($e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
. $safeMessage;
|
||||
}
|
||||
|
||||
private function badge(string $label): string
|
||||
|
||||
@@ -55,9 +55,9 @@ final readonly class AskSseController
|
||||
'includeFullContext' => $includeFullContext,
|
||||
'createdAt' => time(),
|
||||
]);
|
||||
} catch (\Throwable) {
|
||||
} catch (\Throwable $e) {
|
||||
return new JsonResponse(
|
||||
['error' => 'Stream job could not be created.'],
|
||||
['error' => 'Stream job could not be created: ' . $this->formatThrowableForClient($e)],
|
||||
Response::HTTP_INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
@@ -139,6 +139,7 @@ final readonly class AskSseController
|
||||
?Response $cookieResponse
|
||||
): void {
|
||||
$this->prepareStreamRuntime();
|
||||
$this->registerStreamShutdownErrorHandler();
|
||||
|
||||
if ($cookieResponse !== null) {
|
||||
foreach ($cookieResponse->headers->getCookies() as $cookie) {
|
||||
@@ -167,13 +168,51 @@ final readonly class AskSseController
|
||||
} catch (\Throwable $e) {
|
||||
$this->sendEvent(
|
||||
'error',
|
||||
'❌ Stream abgebrochen: ' . $e->getMessage()
|
||||
'❌ Stream abgebrochen: ' . $this->formatThrowableForClient($e)
|
||||
);
|
||||
}
|
||||
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
}
|
||||
|
||||
private function registerStreamShutdownErrorHandler(): void
|
||||
{
|
||||
register_shutdown_function(function (): void {
|
||||
$error = error_get_last();
|
||||
|
||||
if ($error === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fatalTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
|
||||
|
||||
if (!in_array((int) ($error['type'] ?? 0), $fatalTypes, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
'❌ Fataler Serverfehler: %s in %s:%s',
|
||||
(string) ($error['message'] ?? 'unknown error'),
|
||||
(string) ($error['file'] ?? 'unknown file'),
|
||||
(string) ($error['line'] ?? '?')
|
||||
);
|
||||
|
||||
$this->sendEvent('error', $message);
|
||||
$this->sendEvent('done', '[DONE]');
|
||||
});
|
||||
}
|
||||
|
||||
private function formatThrowableForClient(\Throwable $e): string
|
||||
{
|
||||
$message = trim($e->getMessage());
|
||||
|
||||
if ($message === '') {
|
||||
$message = $e::class;
|
||||
}
|
||||
|
||||
return preg_replace('/\s+/u', ' ', $message) ?? $message;
|
||||
}
|
||||
|
||||
private function prepareStreamRuntime(): void
|
||||
{
|
||||
@set_time_limit(0);
|
||||
|
||||
@@ -83,19 +83,32 @@ final readonly class StoreApiClient
|
||||
{
|
||||
$preview = mb_substr(trim($content), 0, 1000);
|
||||
$utf8Failure = $this->containsUtf8FailureSignal($preview);
|
||||
$serverFailure = $statusCode >= 500;
|
||||
$normalizedPreview = mb_strtolower($preview, 'UTF-8');
|
||||
$accessKeyFailure = $statusCode === 401
|
||||
|| $statusCode === 403
|
||||
|| str_contains($preview, 'FRAMEWORK__API_INVALID_ACCESS_KEY')
|
||||
|| str_contains($normalizedPreview, 'access key is invalid');
|
||||
$serverFailure = $statusCode >= 500 || $accessKeyFailure;
|
||||
|
||||
$hint = '';
|
||||
|
||||
if ($accessKeyFailure) {
|
||||
$hint = ' Hint: The configured Shopware Sales Channel access key is invalid or does not match the Store API endpoint.';
|
||||
} elseif ($utf8Failure) {
|
||||
$hint = ' Hint: The request body was valid JSON; this Shopware error usually means Shopware failed while encoding response/product data as UTF-8.';
|
||||
}
|
||||
|
||||
return new StoreApiException(
|
||||
sprintf(
|
||||
'Shopware Store API request failed with status %d. Response: %s%s',
|
||||
$statusCode,
|
||||
$preview,
|
||||
$utf8Failure ? ' Hint: The request body was valid JSON; this Shopware error usually means Shopware failed while encoding response/product data as UTF-8.' : ''
|
||||
$hint
|
||||
),
|
||||
$statusCode,
|
||||
$serverFailure,
|
||||
$utf8Failure,
|
||||
$serverFailure || $utf8Failure
|
||||
$serverFailure || $utf8Failure || $accessKeyFailure
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,10 @@ final class StoreApiException extends RuntimeException
|
||||
|
||||
public function isSystemFailure(): bool
|
||||
{
|
||||
return $this->serverFailure || $this->utf8Failure;
|
||||
if ($this->serverFailure || $this->utf8Failure) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->statusCode === 401 || $this->statusCode === 403;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user