-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Add console.ERROR event and deprecate console.EXCEPTION #18140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having this marker, can't we just set the error as null to signify that error handling is done? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it that way before, but @weaverryan suggested to use a marker:
|
||
{ | ||
$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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ class ConsoleEvent extends Event | |
private $input; | ||
private $output; | ||
|
||
public function __construct(Command $command, InputInterface $input, OutputInterface $output) | ||
public function __construct(Command $command = null, InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->command = $command; | ||
$this->input = $input; | ||
|
@@ -38,7 +38,7 @@ public function __construct(Command $command, InputInterface $input, OutputInter | |
/** | ||
* Gets the command that is executed. | ||
* | ||
* @return Command A Command instance | ||
* @return Command|null A Command instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, this is a BC break. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, but this only happends on the new console.error event. All other events behave the same. |
||
*/ | ||
public function getCommand() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,24 @@ | |
* Allows to handle exception thrown in a command. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* | ||
* @deprecated ConsoleExceptionEvent is deprecated since version 3.3 and will be removed in 4.0. Use ConsoleErrorEvent instead. | ||
*/ | ||
class ConsoleExceptionEvent extends ConsoleEvent | ||
{ | ||
private $exception; | ||
private $exitCode; | ||
private $handled = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wouterj this prop doesn't seem to be used, should we remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, seems to be a left-over of the previous implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 2ad5923 |
||
|
||
public function __construct(Command $command, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode) | ||
public function __construct(Command $command, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode, $deprecation = true) | ||
{ | ||
if ($deprecation) { | ||
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use the ConsoleErrorEvent instead.', __CLASS__), E_USER_DEPRECATED); | ||
} | ||
|
||
parent::__construct($command, $input, $output); | ||
|
||
$this->setException($exception); | ||
$this->exception = $exception; | ||
$this->exitCode = (int) $exitCode; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With everything on master, if you run:
bin/console mldsqlkmldmksq
you got this:It's weird nobody notice that. May be I'm missing something but I really doubt, because all my repository are clean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into this yesterday, I believe the same occurs on 2.7 with ConsoleExceptionEvent. Not sure about the better approach to fix it yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My attempts to fix that: #22144