-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Use convention to allow throwing from __toString() #15077
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
Conversation
@@ -480,7 +480,7 @@ public function __toString() | |||
try { | |||
$content = $this->getContent(); | |||
} catch (\LogicException $e) { | |||
trigger_error($e->getMessage(), E_USER_ERROR); | |||
return trigger_error($e, E_USER_ERROR); |
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.
trigger_error expects a string as first parameter and not an exception?! Also this changes the return value of the method. The method is supposed to return a string and not a boolean from trigger_error.
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.
So this is a bc break.
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.
How that?
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.
The old return value is not returned anymore.
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.
This is always the last executed line of the method, and return is bypassed (see #15077 (comment))
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.
it depends on which error handler is used.
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.
You may be did not notice that the very line (the original trigger_error($e->getMessage(), E_USER_ERROR);
) has been introduced a few days ago. It is not in any tagged version of Symfony. And the original intent and behavior (even before this very line was introduced itself) was really to stop the execution flow at this line.
In any case, About the argument type, $e is automatically casted to a string (__toString() is in the Throwable interface) and this is the most accurate error message: the generated string contains the type of the exception, its message + stack trace excerpt. They are all required to debug correctly. $e->getMessage() is not enought. |
It's not possible to gracefully handle an E_USER_ERROR and continue the programm? |
Technically it's possible with a custom error handler, but that would be wrong as it's not the semantic of E_USER_ERROR. |
The semantic of trigger_error is that it allows to continue the program flow of the method (which is the main difference to an exception). So |
This is not the semantic for E_USER_ERROR: http://3v4l.org/6rDU4 |
Which is also why E_USER_ERROR (and E_RECOVERABLE ERROR) can't be made to continue, but are forced to throw:
|
But this is only the default error handler. A custom one can do things differently. And for those the behavior changes. This is probably an edge case, but for me this code stand-alone makes no sense. So we can just as well provide a convenience method in the debug component for |
When looking for the perfect convention that would allow working around the restriction, I had in mind to not mandate a dependency on the debug component. I still think this is very important. |
It's ugly but a sensible workaround. 👍 |
That's why I like this Debug component :) |
…_toString() (nicolas-grekas) This PR was merged into the 2.3 branch. Discussion ---------- [HttpFoundation] Use convention to allow throwing from __toString() | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Corollary to #15076, works without it. Commits ------- 8982c32 [HttpFoundation] Use convention to allow throwing from __toString()
Corollary to #15076, works without it.