Skip to content

Commit

Permalink
Merge pull request #9538 from WyriHaximus-labs/reactphp-handle-uncaug…
Browse files Browse the repository at this point in the history
…ht-errors

[ReactPHP] Handle uncaught errors by sending their error message as response back
  • Loading branch information
msmith-techempower authored Jan 20, 2025
2 parents 92cf36e + 4541529 commit 27012ed
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(500)),
);
};
}

Expand Down

0 comments on commit 27012ed

Please sign in to comment.