diff --git a/Application.php b/Application.php index ea4f3ec83..07c3c3ca0 100644 --- a/Application.php +++ b/Application.php @@ -121,13 +121,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) : 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)) { @@ -763,20 +760,77 @@ public static function getAbbreviations(array $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) : 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) : 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 5a746c783..969d82795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,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 -----