From 402ebb955b92e7b2088a319744e29b884b1ca57b Mon Sep 17 00:00:00 2001 From: dewanakl Date: Fri, 31 May 2024 13:25:55 +0700 Subject: [PATCH] feat: update error class --- src/Core/Facades/Web.php | 23 +++++++++-------------- src/Core/Support/Error.php | 21 +++++++++++++++------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Core/Facades/Web.php b/src/Core/Facades/Web.php index b418642..a444d6e 100644 --- a/src/Core/Facades/Web.php +++ b/src/Core/Facades/Web.php @@ -97,10 +97,7 @@ private function process(array $route): mixed } $result = $this->app->make(Middleware::class, [$middlewares]) - ->handle( - $this->request, - $this->coreMiddleware($controller, $function) - ); + ->handle($this->request, $this->coreMiddleware($controller, $function)); $error = error_get_last(); if ($error !== null) { @@ -177,10 +174,10 @@ private function runRoute(): mixed protected function handleHttpException(HttpException $th): int { try { - $this->respond->send($this->respond->transform($th->__toString())); + $this->respond->send($th->__toString()); } catch (Throwable $th) { $this->respond->clean(); - $this->respond->send($this->respond->transform($this->handleError($th))); + $this->respond->send($this->handleError($th)); } finally { return 1; } @@ -195,16 +192,14 @@ protected function handleHttpException(HttpException $th): int protected function handleError(Throwable $th): mixed { try { - $kernel = $this->kernel->error(); - $kernelError = new $kernel($th); - $kernelError->report(); + $kernelError = $this->app->make($this->kernel->error()); + $kernelError->setThrowable($th)->report(); // Force close stream. $kernelError->__destruct(); - return $kernelError->render($th); - } catch (Throwable $th) { - $error = new Error($th); - return $error->report()->render($th); + return $kernelError->render(); + } catch (Throwable $t) { + return $this->app->make(Error::class)->setThrowable($t)->report()->render(); } } @@ -238,7 +233,7 @@ public function run(): int return $this->handleHttpException($th); } - $this->respond->send($this->respond->transform($this->handleError($th))); + $this->respond->send($this->handleError($th)); return 1; } } diff --git a/src/Core/Support/Error.php b/src/Core/Support/Error.php index 90c12de..bcfbf4b 100644 --- a/src/Core/Support/Error.php +++ b/src/Core/Support/Error.php @@ -56,12 +56,10 @@ class Error /** * Init object. * - * @param Throwable $throwable * @return void */ - public function __construct(Throwable $throwable) + public function __construct() { - $this->throwable = $throwable; $this->stream = fopen('php://stderr', 'wb'); } @@ -119,6 +117,18 @@ protected function view(string $path, array $data = []): View return $view; } + /** + * Set Throwable. + * + * @param Throwable $t + * @return Error + */ + public function setThrowable(Throwable $t): Error + { + $this->throwable = $t; + return $this; + } + /** * Get Throwable. * @@ -196,10 +206,9 @@ public function report(): Error /** * Show error to dev. * - * @param Throwable $th * @return mixed */ - public function render(Throwable $th): mixed + public function render(): mixed { if (!debug()) { return unavailable(); @@ -209,7 +218,7 @@ public function render(Throwable $th): mixed respond()->setCode(Respond::HTTP_INTERNAL_SERVER_ERROR); if (!request()->ajax()) { - return render(helper_path('/errors/trace'), ['error' => $th]); + return render(helper_path('/errors/trace'), ['error' => $this->throwable]); } respond()->getHeader()->set('Content-Type', 'application/json');