From e8d9c1b61b5005bc445778af8b66f87377fd7916 Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Fri, 6 May 2022 14:53:40 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Allow=20anything=20=E2=80=9CThrowable?= =?UTF-8?q?=E2=80=9D=20to=20be=20reported=20by=20the=20exception=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/Handler.php | 7 ++++--- src/Exceptions/HandlerInterface.php | 5 +++-- tests/Unit/Exceptions/HandlerTest.php | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index 045b6f1..898d781 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace Rareloop\Lumberjack\Exceptions; use Exception; +use Throwable; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Rareloop\Lumberjack\Application; @@ -22,7 +23,7 @@ public function __construct(Application $app) $this->app = $app; } - public function report(Exception $e) + public function report(Throwable $e) { if ($this->shouldNotReport($e)) { return; @@ -34,7 +35,7 @@ public function report(Exception $e) } } - public function render(ServerRequestInterface $request, Exception $e) : ResponseInterface + public function render(ServerRequestInterface $request, Exception $e): ResponseInterface { $e = FlattenException::create($e); @@ -43,7 +44,7 @@ public function render(ServerRequestInterface $request, Exception $e) : Response return new HtmlResponse($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders()); } - protected function shouldNotReport(Exception $e) + protected function shouldNotReport(Throwable $e) { return in_array(get_class($e), $this->dontReport); } diff --git a/src/Exceptions/HandlerInterface.php b/src/Exceptions/HandlerInterface.php index 87509b3..03212c1 100644 --- a/src/Exceptions/HandlerInterface.php +++ b/src/Exceptions/HandlerInterface.php @@ -3,12 +3,13 @@ namespace Rareloop\Lumberjack\Exceptions; use Exception; +use Throwable; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; interface HandlerInterface { - public function report(Exception $e); + public function report(Throwable $e); - public function render(ServerRequestInterface $request, Exception $e) : ResponseInterface; + public function render(ServerRequestInterface $request, Exception $e): ResponseInterface; } diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index dda8be0..44bed05 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -32,6 +32,22 @@ public function report_should_log_exception() $handler->report($exception); } + /** @test */ + public function report_should_log_typeerror() + { + $app = new Application; + + $exception = new \TypeError('Test Type Error'); + + $logger = Mockery::mock(Logger::class); + $logger->shouldReceive('error')->with($exception)->once(); + $app->bind('logger', $logger); + + $handler = new Handler($app); + + $handler->report($exception); + } + /** @test */ public function blacklisted_exception_types_will_not_be_logged() { From d2bb48e5b207d69429442873e812505130b498cb Mon Sep 17 00:00:00 2001 From: Adam Tomat Date: Fri, 6 May 2022 14:58:47 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Allow=20any=20=E2=80=9CThrowable=E2=80=9D?= =?UTF-8?q?=20errors=20can=20be=20can=20be=20renderd=20by=20the=20exceptio?= =?UTF-8?q?n=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/Handler.php | 4 ++-- src/Exceptions/HandlerInterface.php | 2 +- tests/Unit/Exceptions/HandlerTest.php | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index 898d781..9a2cbf7 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -35,9 +35,9 @@ public function report(Throwable $e) } } - public function render(ServerRequestInterface $request, Exception $e): ResponseInterface + public function render(ServerRequestInterface $request, Throwable $e): ResponseInterface { - $e = FlattenException::create($e); + $e = FlattenException::createFromThrowable($e); $handler = new SymfonyExceptionHandler(Config::get('app.debug', false)); diff --git a/src/Exceptions/HandlerInterface.php b/src/Exceptions/HandlerInterface.php index 03212c1..a0ba744 100644 --- a/src/Exceptions/HandlerInterface.php +++ b/src/Exceptions/HandlerInterface.php @@ -11,5 +11,5 @@ interface HandlerInterface { public function report(Throwable $e); - public function render(ServerRequestInterface $request, Exception $e): ResponseInterface; + public function render(ServerRequestInterface $request, Throwable $e): ResponseInterface; } diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 44bed05..c48da20 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -81,6 +81,23 @@ public function render_should_return_an_html_response_when_debug_is_enabled() $this->assertInstanceOf(HtmlResponse::class, $response); } + /** @test */ + public function render_should_return_an_html_response_for_throwables() + { + $app = new Application; + FacadeFactory::setContainer($app); + $config = new Config; + $config->set('app.debug', true); + $app->bind('config', $config); + + $exception = new \TypeError('Test Type Error'); + $handler = new Handler($app); + + $response = $handler->render(new ServerRequest, $exception); + + $this->assertInstanceOf(HtmlResponse::class, $response); + } + /** @test */ public function render_should_return_an_html_response_when_debug_is_disabled() {