Skip to content

Commit 5c2f123

Browse files
authored
feat: handle with handleHttpException (#306)
tranform from httpException to response view
1 parent 2a53c79 commit 5c2f123

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/System/Integrate/Exceptions/Handler.php

+26
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace System\Integrate\Exceptions;
66

7+
use Exception;
78
use System\Container\Container;
89
use System\Http\Exceptions;
910
use System\Http\Request;
1011
use System\Http\Response;
1112
use System\Integrate\Http\Exception\HttpException;
13+
use System\View\Templator;
1214

1315
class Handler
1416
{
@@ -47,6 +49,14 @@ public function render(Request $request, \Throwable $th): Response
4749
return $this->handleJsonResponse($th);
4850
}
4951

52+
if ($th instanceof Exceptions\HttpResponse) {
53+
return $th->getResponse();
54+
}
55+
56+
if ($th instanceof HttpException) {
57+
return $this->handleHttpException($th);
58+
}
59+
5060
throw $th;
5161
}
5262

@@ -102,6 +112,22 @@ protected function handleJsonResponse(\Throwable $th): Response
102112
return $respone->json();
103113
}
104114

115+
protected function handleHttpException(HttpException $e): Response
116+
{
117+
/** @var Templator */
118+
$view = $this->app->make('view.instance');
119+
120+
$code = $view->viewExist('pages/' . $e->getStatusCode())
121+
? $e->getStatusCode()
122+
: 500;
123+
124+
$response = view('pages/' . $code);
125+
$response->setResponeCode($e->getStatusCode());
126+
$response->headers->add($e->getHeaders());
127+
128+
return $response;
129+
}
130+
105131
private function isDev(): bool
106132
{
107133
return 'dev' === $this->app->get('environment');

tests/Integrate/Exceptions/HandlerTest.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use System\Integrate\Exceptions\Handler;
1212
use System\Integrate\Http\Exception\HttpException;
1313
use System\Integrate\Http\Karnel;
14+
use System\Text\Str;
15+
use System\View\Templator;
1416

1517
final class HandlerTest extends TestCase
1618
{
@@ -129,7 +131,26 @@ public function itCanRenderJsonForDev()
129131
$this->assertEquals('Test Exception', $content['messages']['message']);
130132
$this->assertEquals('System\Integrate\Http\Exception\HttpException', $content['messages']['exception']);
131133
// skip meggase.file issue test with diferent platform
132-
$this->assertEquals(44, $content['messages']['line']);
134+
$this->assertEquals(46, $content['messages']['line']);
133135
$this->assertEquals(500, $response->getStatusCode());
134136
}
137+
138+
/** @test */
139+
public function itCanRenderHttpException()
140+
{
141+
$this->app->set('view.instance', fn () => new Templator(__DIR__ . '/assets/', __DIR__ . '/assets/'));
142+
$this->app->set(
143+
'view.response',
144+
fn () => fn (string $view, array $data = []): Response => (new Response())->setContent(
145+
$this->app->make('view.instance')->render("{$view}.template.php", $data)
146+
)
147+
);
148+
149+
$handler = $this->app->make(Handler::class);
150+
151+
$exception = new HttpException(500, 'Internal Error', null, []);
152+
$render = (fn () => $this->{'handleHttpException'}($exception))->call($handler);
153+
154+
$this->assertTrue(Str::contains($render->getContent(), '<h1>Success</h1>'));
155+
}
135156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Success</h1>

0 commit comments

Comments
 (0)