Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #18140 [Console] Add console.ERROR event and deprecate consol…
…e.EXCEPTION (wouterj) This PR was merged into the 3.3-dev branch. Discussion ---------- [Console] Add console.ERROR event and deprecate console.EXCEPTION | Q | A | | --- | --- | | Branch | master | | Bug fix? | yes | | New feature? | yes | | BC breaks? | no | | Deprecations? | yes | | Tests pass? | yes | | Fixed tickets | - | | License | MIT | | Doc PR | todo | ## The Problem The current `console.EXCEPTION` event is only dispatched for exceptions during the execution of `Command#execute()`. All other exceptions (e.g. the ones thrown by listeners to events) are catched by the `try ... catch` loop in `Application#doRunCommand()`. This means that there is _no way to override exception handling_. ## The Solution This PR adds a `console.ERROR` event which has the same scope as the default `try ... catch` loop. This allows to customize all exception handling. In order to keep BC, a new event was created and `console.EXCEPTION` was deprecated. Commits ------- c02a4c9 Added a console.ERROR event
- Loading branch information
Showing
12 changed files
with
298 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/Symfony/Component/Console/Event/ConsoleErrorEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Event; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Debug\Exception\FatalThrowableError; | ||
|
||
/** | ||
* Allows to handle throwables thrown while running a command. | ||
* | ||
* @author Wouter de Jong <wouter@wouterj.nl> | ||
*/ | ||
class ConsoleErrorEvent extends ConsoleExceptionEvent | ||
{ | ||
private $error; | ||
private $handled = false; | ||
|
||
public function __construct(Command $command, InputInterface $input, OutputInterface $output, $error, $exitCode) | ||
{ | ||
if (!$error instanceof \Throwable && !$error instanceof \Exception) { | ||
throw new InvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.', is_object($error) ? get_class($error) : gettype($error))); | ||
} | ||
|
||
$exception = $error; | ||
if (!$error instanceof \Exception) { | ||
$exception = new FatalThrowableError($error); | ||
} | ||
parent::__construct($command, $input, $output, $exception, $exitCode, false); | ||
|
||
$this->error = $error; | ||
} | ||
|
||
/** | ||
* Returns the thrown error/exception. | ||
* | ||
* @return \Throwable | ||
*/ | ||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
/** | ||
* Replaces the thrown error/exception. | ||
* | ||
* @param \Throwable $error | ||
*/ | ||
public function setError($error) | ||
{ | ||
if (!$error instanceof \Throwable && !$error instanceof \Exception) { | ||
throw new InvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.', is_object($error) ? get_class($error) : gettype($error))); | ||
} | ||
|
||
$this->error = $error; | ||
} | ||
|
||
/** | ||
* Marks the error/exception as handled. | ||
* | ||
* If it is not marked as handled, the error/exception will be displayed in | ||
* the command output. | ||
*/ | ||
public function markErrorAsHandled() | ||
{ | ||
$this->handled = true; | ||
} | ||
|
||
/** | ||
* Whether the error/exception is handled by a listener or not. | ||
* | ||
* If it is not yet handled, the error/exception will be displayed in the | ||
* command output. | ||
* | ||
* @return bool | ||
*/ | ||
public function isErrorHandled() | ||
{ | ||
return $this->handled; | ||
} | ||
|
||
/** | ||
* @deprecated Since version 3.3, to be removed in 4.0. Use getError() instead | ||
*/ | ||
public function getException() | ||
{ | ||
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent::getError() instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
return parent::getException(); | ||
} | ||
|
||
/** | ||
* @deprecated Since version 3.3, to be removed in 4.0. Use setError() instead | ||
*/ | ||
public function setException(\Exception $exception) | ||
{ | ||
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent::setError() instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
parent::setException($exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.