Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle general excaption #371

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
SonyPradana marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading