From 4541529762fe4dd3ff89d835f6762cb652bcfbf4 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 15 Jan 2025 22:59:13 +0100 Subject: [PATCH] [ReactPHP] Handle uncaught errors by sending their error message as response back --- frameworks/PHP/reactphp/app.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frameworks/PHP/reactphp/app.php b/frameworks/PHP/reactphp/app.php index 4f9dd816652..bea0c65038d 100644 --- a/frameworks/PHP/reactphp/app.php +++ b/frameworks/PHP/reactphp/app.php @@ -10,6 +10,7 @@ use React\Promise\PromiseInterface; use function React\Promise\all; +use function React\Promise\resolve; /** @return Closure(Request):ResponseInterface */ function requestHandler(): Closure @@ -29,7 +30,7 @@ function requestHandler(): Closure }; return static function (Request $request) use ($world, $fortune, $update): ResponseInterface | PromiseInterface { - return match($request->getUri()->getPath()) { + return resolve((match($request->getUri()->getPath()) { '/plaintext' => Response::plaintext('Hello, World!'), '/json' => Response::json(['message' => 'Hello, World!']), '/db' => db($world), @@ -38,7 +39,9 @@ function requestHandler(): Closure '/update' => updateraw(queryCount($request), $world, $update), // '/info' => info(), default => new Response(404, [], 'Error 404'), - }; + }))->catch( + static fn (Throwable $error): PromiseInterface => resolve(Response::plaintext($error->getMessage())->withStatus(500)), + ); }; }