Skip to content

Commit

Permalink
[ReactPHP] Handle uncaught errors by sending their error message as r…
Browse files Browse the repository at this point in the history
…esponse back
  • Loading branch information
WyriHaximus committed Jan 15, 2025
1 parent 0a9ab06 commit 3014f1f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions frameworks/PHP/reactphp/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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(200)), // This should be a 500, but with a 200 the benchmarking tooling considers the framework alive
);
};
}

Expand Down

0 comments on commit 3014f1f

Please sign in to comment.