From 596ec3189506a9bda95f6086128a08cf83e4761a Mon Sep 17 00:00:00 2001 From: Maxim Melnik Date: Mon, 28 Aug 2023 19:50:27 +0300 Subject: [PATCH] Refactored handling of exceptions --- application/console/app.php | 9 --------- application/src/App.php | 2 ++ application/src/ExceptionHandler.php | 29 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 application/src/ExceptionHandler.php diff --git a/application/console/app.php b/application/console/app.php index c8d444712..39cbd0fdc 100644 --- a/application/console/app.php +++ b/application/console/app.php @@ -9,14 +9,5 @@ try { (new App())->run($argv); } catch (Throwable $exception) { - $trace = json_encode($exception->getTrace(), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - $message = <<getMessage()}' - Code: '{$exception->getCode()}' - Trace: - - $trace - EOL; - fwrite(STDERR, $message . PHP_EOL); } diff --git a/application/src/App.php b/application/src/App.php index 52c7d2d6a..e98af9b46 100644 --- a/application/src/App.php +++ b/application/src/App.php @@ -18,6 +18,8 @@ public function run(array $arguments): void try { (new MainController($request, $config))->index(); + } catch (\Throwable $exception) { + (new ExceptionHandler())->handle($exception); } finally { // additional protection when __destruct will not call in BaseSocket class if ( diff --git a/application/src/ExceptionHandler.php b/application/src/ExceptionHandler.php new file mode 100644 index 000000000..a746a0790 --- /dev/null +++ b/application/src/ExceptionHandler.php @@ -0,0 +1,29 @@ +prepareMessage($exception) . PHP_EOL); + } + + /** + * @throws \JsonException + */ + private function prepareMessage(\Throwable $exception): string + { + $trace = json_encode($exception->getTrace(), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); + + return <<getMessage()}' + Code: '{$exception->getCode()}' + Trace: + + $trace + EOL; + } +}