From 682f4136a60cfafcfa4b360da247d8a3ea9c6bae Mon Sep 17 00:00:00 2001 From: Nathanael Yusuf Tj <26967808+nathanaelytj@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:42:53 +0000 Subject: [PATCH] Stop server if something happen to server --- src/Servers/Reverb/Http/Server.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Servers/Reverb/Http/Server.php b/src/Servers/Reverb/Http/Server.php index 182a849f..1810dc63 100644 --- a/src/Servers/Reverb/Http/Server.php +++ b/src/Servers/Reverb/Http/Server.php @@ -14,6 +14,8 @@ use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; +use function React\Promise\set_rejection_handler; + class Server { use ClosesConnections; @@ -38,6 +40,17 @@ public function __construct(protected ServerInterface $socket, protected Router */ public function start(): void { + /** Set global promise rejection handler */ + set_rejection_handler(function (Throwable $e) { + Log::error($e->getMessage()); + + /** Echo error message */ + echo "Unhandled exception: {$e->getMessage()}\n Server will stop.\n"; + + /** Stop the server */ + $this->stop(); + }); + $this->loop->run(); }