fix sse error handling if shop api error part 3

This commit is contained in:
team2
2026-04-25 20:49:45 +02:00
parent 2044a465ad
commit d13e5537ed
6 changed files with 112 additions and 12 deletions

View File

@@ -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);