exceptions: implemented error rendering return

This commit is contained in:
Igor Scheller 2018-08-07 03:06:21 +02:00
parent 864a086900
commit 92c26718fd
4 changed files with 35 additions and 4 deletions

View File

@ -54,8 +54,10 @@ class Handler
/** /**
* @param Throwable $e * @param Throwable $e
* @param bool $return
* @return string
*/ */
public function exceptionHandler($e) public function exceptionHandler($e, $return = false)
{ {
if (!$this->request instanceof Request) { if (!$this->request instanceof Request) {
$this->request = new Request(); $this->request = new Request();
@ -63,8 +65,19 @@ class Handler
$handler = $this->handler[$this->environment]; $handler = $this->handler[$this->environment];
$handler->report($e); $handler->report($e);
ob_start();
$handler->render($this->request, $e); $handler->render($this->request, $e);
if ($return) {
$output = ob_get_contents();
ob_end_clean();
return $output;
}
http_response_code(500);
ob_end_flush();
$this->die(); $this->die();
return '';
} }
/** /**

View File

@ -34,6 +34,8 @@ class Whoops extends Legacy implements HandlerInterface
$whoops = $this->app->make(WhoopsRunner::class); $whoops = $this->app->make(WhoopsRunner::class);
$handler = $this->getPrettyPageHandler($e); $handler = $this->getPrettyPageHandler($e);
$whoops->pushHandler($handler); $whoops->pushHandler($handler);
$whoops->writeToOutput(false);
$whoops->allowQuit(false);
if ($request->isXmlHttpRequest()) { if ($request->isXmlHttpRequest()) {
$handler = $this->getJsonResponseHandler(); $handler = $this->getJsonResponseHandler();

View File

@ -49,15 +49,19 @@ class HandlerTest extends TestCase
public function testExceptionHandler() public function testExceptionHandler()
{ {
$exception = new Exception(); $exception = new Exception();
$errorMessage = 'Oh noes, an error!';
/** @var HandlerInterface|Mock $handlerMock */ /** @var HandlerInterface|Mock $handlerMock */
$handlerMock = $this->getMockForAbstractClass(HandlerInterface::class); $handlerMock = $this->getMockForAbstractClass(HandlerInterface::class);
$handlerMock->expects($this->once()) $handlerMock->expects($this->atLeastOnce())
->method('report') ->method('report')
->with($exception); ->with($exception);
$handlerMock->expects($this->once()) $handlerMock->expects($this->atLeastOnce())
->method('render') ->method('render')
->with($this->isInstanceOf(Request::class), $exception); ->with($this->isInstanceOf(Request::class), $exception)
->willReturnCallback(function () use ($errorMessage) {
echo $errorMessage;
});
/** @var Handler|Mock $handler */ /** @var Handler|Mock $handler */
$handler = $this->getMockBuilder(Handler::class) $handler = $this->getMockBuilder(Handler::class)
@ -68,7 +72,11 @@ class HandlerTest extends TestCase
$handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock); $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock);
$this->expectOutputString($errorMessage);
$handler->exceptionHandler($exception); $handler->exceptionHandler($exception);
$return = $handler->exceptionHandler($exception, true);
$this->assertEquals($errorMessage, $return);
} }
/** /**

View File

@ -72,6 +72,14 @@ class WhoopsTest extends TestCase
[$prettyPageHandler], [$prettyPageHandler],
[$jsonResponseHandler] [$jsonResponseHandler]
); );
$whoopsRunner
->expects($this->once())
->method('writeToOutput')
->with(false);
$whoopsRunner
->expects($this->once())
->method('allowQuit')
->with(false);
$whoopsRunner $whoopsRunner
->expects($this->once()) ->expects($this->once())
->method('handleException') ->method('handleException')