From e08a39faab8d1f14cec1fe8a844f6604e48f9d17 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Mon, 2 Sep 2024 14:16:32 +0700 Subject: [PATCH 1/2] fix: handle general excaption --- src/System/Integrate/Exceptions/Handler.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/System/Integrate/Exceptions/Handler.php b/src/System/Integrate/Exceptions/Handler.php index 2df721c1..8e1632b1 100644 --- a/src/System/Integrate/Exceptions/Handler.php +++ b/src/System/Integrate/Exceptions/Handler.php @@ -45,10 +45,6 @@ public function __construct(Container $application) */ public function render(Request $request, \Throwable $th): Response { - if ($request->isJson()) { - return $this->handleJsonResponse($th); - } - if ($th instanceof Exceptions\HttpResponse) { return $th->getResponse(); } @@ -57,7 +53,9 @@ public function render(Request $request, \Throwable $th): Response return $this->handleHttpException($th); } - throw $th; + return $request->isJson() + ? $this->handleJsonResponse($th) + : $this->handleResponse($th); } /** @@ -112,6 +110,11 @@ protected function handleJsonResponse(\Throwable $th): Response return $respone->json(); } + protected function handleResponse(\Throwable $th): Response + { + return new Response($th->getMessage(), 500); + } + protected function handleHttpException(HttpException $e): Response { $templator = $this->registerViewPath(); From e96428de4606838f22289ebf73bf6d0ab85c0ad3 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Tue, 3 Sep 2024 14:52:26 +0700 Subject: [PATCH 2/2] throw if debug mode --- src/System/Integrate/Exceptions/Handler.php | 18 ++++++++++++------ tests/Integrate/Exceptions/HandlerTest.php | 9 ++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/System/Integrate/Exceptions/Handler.php b/src/System/Integrate/Exceptions/Handler.php index 8e1632b1..006384e8 100644 --- a/src/System/Integrate/Exceptions/Handler.php +++ b/src/System/Integrate/Exceptions/Handler.php @@ -45,6 +45,10 @@ public function __construct(Container $application) */ public function render(Request $request, \Throwable $th): Response { + if ($request->isJson()) { + return $this->handleJsonResponse($th); + } + if ($th instanceof Exceptions\HttpResponse) { return $th->getResponse(); } @@ -53,9 +57,11 @@ public function render(Request $request, \Throwable $th): Response return $this->handleHttpException($th); } - return $request->isJson() - ? $this->handleJsonResponse($th) - : $this->handleResponse($th); + if (false === $this->isDebug()) { + return $this->handleResponse($th); + } + + throw $th; } /** @@ -95,7 +101,7 @@ protected function handleJsonResponse(\Throwable $th): Response $respone->headers->add($th->getHeaders()); } - if ($this->isDev()) { + if ($this->isDebug()) { return $respone->json([ 'code' => $respone->getStatusCode(), 'messages' => [ @@ -149,8 +155,8 @@ public function registerViewPath(): Templator return $view; } - private function isDev(): bool + private function isDebug(): bool { - return 'dev' === $this->app->get('environment'); + return $this->app->get('app.debug') ?? false; } } diff --git a/tests/Integrate/Exceptions/HandlerTest.php b/tests/Integrate/Exceptions/HandlerTest.php index a7a74f04..09d4af1f 100644 --- a/tests/Integrate/Exceptions/HandlerTest.php +++ b/tests/Integrate/Exceptions/HandlerTest.php @@ -114,7 +114,7 @@ public function itCanReportException() public function itCanRenderJson() { $this->app->bootedCallback(function () { - $this->app->set('environment', 'prod'); + $this->app->set('app.debug', false); }); $karnel = $this->app->make(Karnel::class); @@ -132,9 +132,12 @@ public function itCanRenderJson() } /** @test */ - public function itCanRenderJsonForDev() + public function itCanRenderJsonForDebug() { - $this->app->set('environment', 'dev'); + $this->app->bootedCallback(function () { + $this->app->set('app.debug', true); + }); + $karnel = $this->app->make(Karnel::class); $response = $karnel->handle(new Request('/test', [], [], [], [], [], [ 'content-type' => 'application/json',