From 7489726d9c66aa5673e1bc14aa21e3624e668abc Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Thu, 8 Aug 2019 18:27:37 +0200 Subject: [PATCH] Deprecate things that prevent \Throwable from bubbling down --- Application.php | 64 +++++++++++++++++++++++++++++++++++++++++++++---- CHANGELOG.md | 2 ++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Application.php b/Application.php index 995aceaf1..6fa82815a 100644 --- a/Application.php +++ b/Application.php @@ -128,13 +128,10 @@ public function run(InputInterface $input = null, OutputInterface $output = null } $renderException = function (\Throwable $e) use ($output) { - if (!$e instanceof \Exception) { - $e = class_exists(ErrorException::class) ? new ErrorException($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine())); - } if ($output instanceof ConsoleOutputInterface) { - $this->renderException($e, $output->getErrorOutput()); + $this->renderThrowable($e, $output->getErrorOutput()); } else { - $this->renderException($e, $output); + $this->renderThrowable($e, $output); } }; if ($phpHandler = set_exception_handler($renderException)) { @@ -792,20 +789,77 @@ public static function getAbbreviations($names) /** * Renders a caught exception. + * + * @deprecated since Symfony 4.4, use "renderThrowable()" instead */ public function renderException(\Exception $e, OutputInterface $output) { + @trigger_error(sprintf('The "%s::renderException()" method is deprecated since Symfony 4.4, use "renderThrowable()" instead.', __CLASS__), E_USER_DEPRECATED); + $output->writeln('', OutputInterface::VERBOSITY_QUIET); $this->doRenderException($e, $output); + $this->finishRenderThrowableOrException($output); + } + + public function renderThrowable(\Throwable $e, OutputInterface $output): void + { + if (__CLASS__ !== \get_class($this) && __CLASS__ === (new \ReflectionMethod($this, 'renderThrowable'))->getDeclaringClass()->getName() && __CLASS__ !== (new \ReflectionMethod($this, 'renderException'))->getDeclaringClass()->getName()) { + @trigger_error(sprintf('The "%s::renderException()" method is deprecated since Symfony 4.4, use "renderThrowable()" instead.', __CLASS__), E_USER_DEPRECATED); + + if (!$e instanceof \Exception) { + $e = class_exists(ErrorException::class) ? new ErrorException($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine())); + } + + $this->renderException($e, $output); + + return; + } + + $output->writeln('', OutputInterface::VERBOSITY_QUIET); + + $this->doRenderThrowable($e, $output); + + $this->finishRenderThrowableOrException($output); + } + + private function finishRenderThrowableOrException(OutputInterface $output): void + { if (null !== $this->runningCommand) { $output->writeln(sprintf('%s', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET); $output->writeln('', OutputInterface::VERBOSITY_QUIET); } } + /** + * @deprecated since Symfony 4.4, use "doRenderThrowable()" instead + */ protected function doRenderException(\Exception $e, OutputInterface $output) + { + @trigger_error(sprintf('The "%s::doRenderException()" method is deprecated since Symfony 4.4, use "doRenderThrowable()" instead.', __CLASS__), E_USER_DEPRECATED); + + $this->doActuallyRenderThrowable($e, $output); + } + + protected function doRenderThrowable(\Throwable $e, OutputInterface $output): void + { + if (__CLASS__ !== \get_class($this) && __CLASS__ === (new \ReflectionMethod($this, 'doRenderThrowable'))->getDeclaringClass()->getName() && __CLASS__ !== (new \ReflectionMethod($this, 'doRenderException'))->getDeclaringClass()->getName()) { + @trigger_error(sprintf('The "%s::doRenderException()" method is deprecated since Symfony 4.4, use "doRenderThrowable()" instead.', __CLASS__), E_USER_DEPRECATED); + + if (!$e instanceof \Exception) { + $e = class_exists(ErrorException::class) ? new ErrorException($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine())); + } + + $this->doRenderException($e, $output); + + return; + } + + $this->doActuallyRenderThrowable($e, $output); + } + + private function doActuallyRenderThrowable(\Throwable $e, OutputInterface $output): void { do { $message = trim($e->getMessage()); diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f04325e3..d722a6d5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ CHANGELOG * marked all dispatched event classes as `@final` * added support for displaying table horizontally * deprecated returning `null` from `Command::execute()`, return `0` instead + * Deprecated the `Application::renderException()` and `Application::doRenderException()` methods, + use `renderThrowable()` and `doRenderThrowable()` instead. 4.3.0 -----