Skip to content

[Console] fixed PHP7 Errors are now handled and converted to Exceptions #19813

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

Closed
wants to merge 2 commits into from

Conversation

fonsecas72
Copy link

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #18484
License MIT
Doc PR

@HeahDude
Copy link
Contributor

HeahDude commented Sep 1, 2016

ping @romqin @skigun :)

@TomasVotruba
Copy link
Contributor

Test would be great for this.

$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

throw $e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be throwing the original throwable here. This is a BC break?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Copy link
Author

@fonsecas72 fonsecas72 Sep 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean?
That I should delete the $e = new FatalThrowableError($e); line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose this:

try {
    $e = null;
    $exitCode = $command->run($input, $output);
} catch (\Exception $x) {
    $e = $x;
} catch (\Throwable $x) {
    $e = new FatalThrowableError($x);
}
if (null !== $e) {
    $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
    $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);

    $e = $event->getException();

    $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
    $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

    throw $x;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just add another function and call that at the end of each try catch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try {
    $exitCode = $command->run($input, $output);
} catch (\Exception $e) {
    $this->someFuntion($e);

    throw $e;
} catch (\Throwable $x) {
    $this->someFuntion(new FatalThrowableError($e));

    throw $e;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will go with whatever solution you find better /cc @nicolas-grekas :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pure matter of coding style, I don't care :)

@nicolas-grekas
Copy link
Member

Looks like this is untested, can you add a test case please?
The composer.json file misses a requirement for the debug component now (~2.7).

@fonsecas72
Copy link
Author

@nicolas-grekas it's done I believe, thanks
Is there anything else I can do to improve this?

@fonsecas72 fonsecas72 force-pushed the ticket_18484 branch 2 times, most recently from 999beb4 to a5250fd Compare September 6, 2016 12:53
@@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.3.9"
"php": ">=5.3.9",
"symfony/debug": "~2.7,>=2.7.14"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ~2.7,>=2.7.2

@fonsecas72 fonsecas72 force-pushed the ticket_18484 branch 3 times, most recently from 8e1b521 to 93ae8c6 Compare September 7, 2016 11:59
@@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.3.9"
"php": ">=5.3.9",
"symfony/debug": "~2.7,>=2.7.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^2.7.2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, what about 3.x?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use ^ yet
we'll add 3.x when merging into 2.8 (the only branch that opens to 3.x)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok :)

@fonsecas72
Copy link
Author

fonsecas72 commented Sep 7, 2016

@nicolas-grekas the original exception is being re-throw and tests are updated/fixed now

@@ -914,7 +914,7 @@ public function testRunWithDispatcher()
}

/**
* @expectedException \LogicException
* @expectedException \RuntimeException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my bad, this can't change, that would be a bc break.
I propose to tweak the implementation like this, so that the original exception is preserved, except when a listener decides to alter it:

            try {
                $e = null;
                $exitCode = $command->run($input, $output);
            } catch (\Exception $x) {
                $e = $x;
            } catch (\Throwable $x) {
                $e = new FatalThrowableError($x);
            }
            if (null !== $e) {
                $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
                $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);

                if ($e !== $event->getException()) {
                    $x = $e = $event->getException();
                }

                $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
                $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

                throw $x;
            }

Copy link
Author

@fonsecas72 fonsecas72 Sep 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh now I get it! I've added a little test for that too. I hope everything is ok now :)

@nicolas-grekas
Copy link
Member

👍

@fabpot
Copy link
Member

fabpot commented Sep 8, 2016

Thank you @fonsecas72.

fabpot added a commit that referenced this pull request Sep 8, 2016
…to Exceptions (fonsecas72)

This PR was squashed before being merged into the 2.7 branch (closes #19813).

Discussion
----------

[Console] fixed PHP7 Errors are now handled and converted to Exceptions

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18484
| License       | MIT
| Doc PR        |

Commits
-------

d3c613b [Console] fixed PHP7 Errors are now handled and converted to Exceptions
@fabpot fabpot closed this Sep 8, 2016
@fonsecas72
Copy link
Author

Thank you @TomasVotruba @nicolas-grekas @GrahamCampbell 👍

@fonsecas72 fonsecas72 deleted the ticket_18484 branch September 8, 2016 15:38
@TomasVotruba
Copy link
Contributor

@fonsecas72 Glad to help :)

@GrahamCampbell GrahamCampbell mentioned this pull request Sep 13, 2016
This was referenced Oct 3, 2016
fabpot added a commit that referenced this pull request Dec 5, 2016
…adus)

This PR was squashed before being merged into the 2.7 branch (closes #20736).

Discussion
----------

[Console] fixed PHP7 Errors when not using Dispatcher

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17257, #20110, #20111
| License       | MIT
| Doc PR        | n/a

Original fix, #19813, works only when there is event dispatcher available.
This PR fix the issue also for scenario without event dispatcher.

Closes #20110 issue and #20111 PR connected to it.
Closing #17257 , as everywhere the error is converted to exception and it should be handled

Commits
-------

899fa79 [Console] fixed PHP7 Errors when not using Dispatcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants