diff --git a/src/Whoops/Run.php b/src/Whoops/Run.php index cfaaa5e..77ec212 100644 --- a/src/Whoops/Run.php +++ b/src/Whoops/Run.php @@ -303,38 +303,40 @@ final class Run implements RunInterface // we might want to send it straight away to the client, // or return it silently. $this->system->startOutputBuffering(); - + // Just in case there are no handlers: $handlerResponse = null; $handlerContentType = null; - foreach ($this->handlerQueue as $handler) { - $handler->setRun($this); - $handler->setInspector($inspector); - $handler->setException($exception); + try { + foreach ($this->handlerQueue as $handler) { + $handler->setRun($this); + $handler->setInspector($inspector); + $handler->setException($exception); - // The HandlerInterface does not require an Exception passed to handle() - // and neither of our bundled handlers use it. - // However, 3rd party handlers may have already relied on this parameter, - // and removing it would be possibly breaking for users. - $handlerResponse = $handler->handle($exception); + // The HandlerInterface does not require an Exception passed to handle() + // and neither of our bundled handlers use it. + // However, 3rd party handlers may have already relied on this parameter, + // and removing it would be possibly breaking for users. + $handlerResponse = $handler->handle($exception); - // Collect the content type for possible sending in the headers. - $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; + // Collect the content type for possible sending in the headers. + $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; - if (in_array($handlerResponse, [Handler::LAST_HANDLER, Handler::QUIT])) { - // The Handler has handled the exception in some way, and - // wishes to quit execution (Handler::QUIT), or skip any - // other handlers (Handler::LAST_HANDLER). If $this->allowQuit - // is false, Handler::QUIT behaves like Handler::LAST_HANDLER - break; + if (in_array($handlerResponse, [Handler::LAST_HANDLER, Handler::QUIT])) { + // The Handler has handled the exception in some way, and + // wishes to quit execution (Handler::QUIT), or skip any + // other handlers (Handler::LAST_HANDLER). If $this->allowQuit + // is false, Handler::QUIT behaves like Handler::LAST_HANDLER + break; + } } + + $willQuit = $handlerResponse == Handler::QUIT && $this->allowQuit(); + } finally { + $output = $this->system->cleanOutputBuffer(); } - $willQuit = $handlerResponse == Handler::QUIT && $this->allowQuit(); - - $output = $this->system->cleanOutputBuffer(); - // If we're allowed to, send output generated by handlers directly // to the output, otherwise, and if the script doesn't quit, return // it so that it may be used by the caller diff --git a/tests/Whoops/Handler/PlainTextHandlerTest.php b/tests/Whoops/Handler/PlainTextHandlerTest.php index 4b92fcc..3432c91 100644 --- a/tests/Whoops/Handler/PlainTextHandlerTest.php +++ b/tests/Whoops/Handler/PlainTextHandlerTest.php @@ -63,10 +63,13 @@ class PlainTextHandlerTest extends TestCase $run->register(); $exception = $exception ?: $this->getException(); - ob_start(); - $run->handleException($exception); - - return ob_get_clean(); + + try { + ob_start(); + $run->handleException($exception); + } finally { + return ob_get_clean(); + } } /** @@ -266,11 +269,11 @@ class PlainTextHandlerTest extends TestCase RuntimeException::class, 'Outer exception message', __FILE__, - 258, + 261, "\nCaused by\n" . RuntimeException::class, 'Inner exception message', __FILE__, - 258 + 261 ), $text );