Skip to content

Commit

Permalink
fix: handle general exceptions (#371)
Browse files Browse the repository at this point in the history
* fix: handle general excaption

* throw if debug mode
  • Loading branch information
SonyPradana authored Sep 3, 2024
1 parent e6782f2 commit dab34bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/System/Integrate/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function render(Request $request, \Throwable $th): Response
return $this->handleHttpException($th);
}

if (false === $this->isDebug()) {
return $this->handleResponse($th);
}

throw $th;
}

Expand Down Expand Up @@ -97,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' => [
Expand All @@ -112,6 +116,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();
Expand Down Expand Up @@ -146,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;
}
}
9 changes: 6 additions & 3 deletions tests/Integrate/Exceptions/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
Expand Down

0 comments on commit dab34bf

Please sign in to comment.