Skip to content

[Debug] Support any Throwable object in FlattenException #26528

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

Merged
merged 1 commit into from
Apr 6, 2018

Conversation

derrabus
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #26429
License MIT
Doc PR N/A

Alternative to #26447 without BC breaks, follows #26028.

This PR removes the need to convert Throwable objects into exceptions when working with FlattenException.

ping @instabledesign @ostrolucky @nicolas-grekas

@derrabus derrabus changed the title Support any Throwable object in FlattenException [Debug] Support any Throwable object in FlattenException Mar 14, 2018
@derrabus derrabus force-pushed the flatten-exception-with-throwable branch from 4813746 to ba00693 Compare March 14, 2018 14:15
*
* @return static
*/
public static function create($throwable, $statusCode = null, array $headers = array())
Copy link
Member

Choose a reason for hiding this comment

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

your #26447 (comment) comment applies as well, removing a typehint breaks childs

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, it will raise a warning. I'll add another deprecation then.

@derrabus derrabus force-pushed the flatten-exception-with-throwable branch 4 times, most recently from 10cef07 to 9a2cf0c Compare March 14, 2018 15:49
@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Mar 14, 2018
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
{
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1. Use "createFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should not deprecate this method and save us the version bumps?

Copy link
Member

Choose a reason for hiding this comment

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

a lot of code in this class will never be reached in case the throwable is an \Error (see instanceof *ExtensionInterface checks).
If we really want a flatten error, couldn't it just be a new FlattenError? extracting the reusable logic in a trait if any

Copy link
Member

Choose a reason for hiding this comment

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

Tbh, given the error handling in place I fear that anyways the result will be more confusing than useful here, perhaps it's just me

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for FlattenError

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not expect those version bumps to be a problem, but we can add the depreciation at a later point, I guess. I'm going to remove the deprecation an revert the changes I made to other components.

Copy link
Member Author

Choose a reason for hiding this comment

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

If I added a separate class for errors, I would need to duplicate a lot of code, lose the ability to properly type-hint for FlattenException and I'd still have to do type-switches when working with arbitrary throwables. Sorry, but this is really a bad idea, imho.

@derrabus derrabus force-pushed the flatten-exception-with-throwable branch from 9a2cf0c to f2782ad Compare March 15, 2018 08:42
@derrabus
Copy link
Member Author

I've remove the deprecation of the create method and rolled back the changes in TwigBundle and HttpKernel for now, as @nicolas-grekas suggested. PR is ready.

@@ -34,88 +34,94 @@ class FlattenExceptionTest extends TestCase
{
public function testStatusCode()
{
$flattened = FlattenException::create(new \RuntimeException(), 403);
$flattened = FlattenException::createFromThrowable(new \RuntimeException(), 403);
Copy link
Member

Choose a reason for hiding this comment

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

unless required, these changes should also be reverted, this will help merges as usual

Copy link
Member Author

Choose a reason for hiding this comment

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

Understandable, although I'd prefer to keep the test as it is. I'll change it.

@derrabus derrabus force-pushed the flatten-exception-with-throwable branch 3 times, most recently from 4fdb120 to 47b06a7 Compare March 15, 2018 09:11
@derrabus
Copy link
Member Author

All right, the change set should be minimal now. Still, my goal would be to remove the create method with Symfony 5, because it's redundant now.

{
$e = new static();
$e->setMessage($exception->getMessage());
Copy link
Member

Choose a reason for hiding this comment

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

sorry, anynother change: let's keep the old name, it's still fine

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough.

@derrabus
Copy link
Member Author

Status: Needs Review

Copy link
Contributor

@instabledesign instabledesign left a comment

Choose a reason for hiding this comment

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

Sounds good.

@derrabus derrabus force-pushed the flatten-exception-with-throwable branch from f78a15a to 25e5cdf Compare March 16, 2018 10:49
@derrabus
Copy link
Member Author

The failure on Travis looks unrelated.

@derrabus
Copy link
Member Author

Anything left to do here?

return static::createFromThrowable($exception, $statusCode, $headers);
}

public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = array()): self
Copy link
Member

Choose a reason for hiding this comment

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

What about using new instead (FlattenException::new() looks better to me)?

Copy link
Member

@nicolas-grekas nicolas-grekas Apr 2, 2018

Choose a reason for hiding this comment

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

it's not possible to call a function "new" because the keyword is reserved...

Copy link
Member

Choose a reason for hiding this comment

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

Works in php 7 https://3v4l.org/N5iEr

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, then the developer would have the choice between create() and new() which looks rather confusing to me. Also, I would not recommend to use keywords as method names even if it currently works.

public function setTraceFromException(\Exception $exception)
{
$this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1. Use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
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 ... since Symfony 4.1, use ...

@derrabus derrabus force-pushed the flatten-exception-with-throwable branch from 25e5cdf to 96c1a6f Compare April 5, 2018 17:04
@fabpot
Copy link
Member

fabpot commented Apr 6, 2018

Thank you @derrabus.

@fabpot fabpot merged commit 96c1a6f into symfony:master Apr 6, 2018
fabpot added a commit that referenced this pull request Apr 6, 2018
…on (derrabus)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Debug] Support any Throwable object in FlattenException

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

Alternative to #26447 without BC breaks, follows #26028.

This PR removes the need to convert `Throwable` objects into exceptions when working with `FlattenException`.

ping @instabledesign @ostrolucky @nicolas-grekas

Commits
-------

96c1a6f Support any Throwable object in FlattenException.
@derrabus derrabus deleted the flatten-exception-with-throwable branch April 6, 2018 08:22
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request May 2, 2018
…r (derrabus)

This PR was merged into the master branch.

Discussion
----------

FlattenException might also represent an instance of Error

This PR documents symfony/symfony#26528 and fixes #9636.

Commits
-------

cd309c9 FlattenException might also represent an instance of Error.
@fabpot fabpot mentioned this pull request May 7, 2018
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