fix sse error handling if shop api error part 3
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user