From bb329be419ad9ae298504ad8703063996e9a4ba2 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Wed, 21 Jun 2017 11:47:53 +0200 Subject: [PATCH] trying to fix the fatal error handling #2 --- src/Whoops.php | 16 +++++++++++++++- tests/WhoopsTest.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Whoops.php b/src/Whoops.php index c935b50..1185d8d 100644 --- a/src/Whoops.php +++ b/src/Whoops.php @@ -7,6 +7,7 @@ use Interop\Http\ServerMiddleware\MiddlewareInterface; use Interop\Http\ServerMiddleware\DelegateInterface; use Whoops\Run; +use Whoops\Util\SystemFacade; use Whoops\Handler\PrettyPageHandler; use Whoops\Handler\PlainTextHandler; use Whoops\Handler\JsonResponseHandler; @@ -105,7 +106,20 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele */ private static function getWhoopsInstance(ServerRequestInterface $request) { - $whoops = new Run(); + $system = new SystemFacade(); + $whoops = new Run($system); + + //E_ERROR in PHP 5.x + if (!class_exists('Throwable')) { + $system->registerShutdownFunction(function () use ($whoops) { + $whoops->allowQuit(true); + $whoops->writeToOutput(true); + $whoops->sendHttpCode(true); + + $method = Run::SHUTDOWN_HANDLER; + $whoops->$method(); + }); + } switch (self::getPreferredFormat($request)) { case 'json': diff --git a/tests/WhoopsTest.php b/tests/WhoopsTest.php index 0692308..19bcc90 100644 --- a/tests/WhoopsTest.php +++ b/tests/WhoopsTest.php @@ -22,6 +22,20 @@ function () { $this->assertNotFalse(strpos($response->getBody(), 'Error Processing Request')); } + public function testFatalError() + { + $response = Dispatcher::run([ + new Whoops(), + function () { + new UnexistingClass(); + }, + ]); + + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals('text/plain', $response->getHeaderLine('Content-Type')); + $this->assertNotFalse(strpos($response->getBody(), "Class 'Middlewares\\Tests\\UnexistingClass' not found")); + } + public function testNotError() { $response = Dispatcher::run([