-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Add Method to UnhandledMatchError to get the Unhandled Value #14490
Comments
Hi @donatj! Thanks for the suggestion. I'm not personally in favor, because 1. I don't believe you should generally catch |
Thanks you for the feedback. I do, however, disagree that you shouldn't catch errors. Why would PHP move from warnings to errors to if not to make them more easily catchable/handleable? Being able to catch them is particularly useful I find in reflection/inspection - especially in cases where you code is running someone else code that might not be perfectly formed. Providing as much info as possible helps others fix issues more efficiently. Catching errors allows for graceful error handling, and adding helpful detail only serves makes it more useful. For Open to more thoughts on this. |
Fatal errors are very harsh, and warnings are error prone because they don't terminate control flow. With exceptions you can handle generic errors gracefully in your front controller and display an error message accordingly. I don't think catching specific errors makes much sense, because if you know your code can throw errors, you should fix the code. Of course, you can't know about all errors in advance, but then you also don't know about its type (is it a Note that this reasoning is exactly what we use to determine whether something internally should throw an error or an exception: Do we expect users to want to catch them, or is the problem unrecoverable? For the former, we use exceptions, and for the latter we use errors. Do you have a use case other than logging? Because then the value is already visible in the error message (if it is inspectable, that is). If providing more information is the goal, maybe this is better solved by improving error messages in all places, so all users can benefit. |
I agree with @iluuu1994 here, I'm not really in favour of adding such capabilities. The Some more rationale about this can be found in the original RFC to convert bailouts to Exceptions. [1] As one of the main people that converted |
Well if no one is interested, we can probably close this. |
If logging is the only use-case, feel free to close this issue 🙂 |
No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you. |
Hello, im not sure if it is appropiate to join this discussion now as the issue seems to be closed, but seems like some counterarguments against this feature are based on not completely valid information?
At the moment it is not possible to ge the value in some cases:
Message: So currently you would do what? Catch to get the value?
Message: I agree that it is best to throw own, more informative errors like this:
Message: But the errors are in place because errors still happen because reasons, and it is not always our code, or sometimes even a code we have control over, that does. So back to the original arguments:
My counter argument here is: Saying that a value is not interesting because catching is bad makes as much sense as saying that vegetables are not healthy, because you don't like them. You should catch errors, if the value is "interesting" (important) and catching is the only way to get it.
No, it is not always there, at least not whole. And if your argument is that this is how you do it in other places, then other places are probably a problem aswell.
Yes please. |
I think @WM-Programator-3 's example of handling failed matches outside your projects purview is a great example of a use case |
I still don't understand. If you need to find out why the match has failed, the value passed to the match is already visible in the error message. If you need to understand what values are being accepted, most frameworks will print excerpts of the stack trace, with the first frame being the match in this case. Adding the default value to the exception would not help for this case anyway.
But does it? Catching a random Anyway, this issue has already been discussed too much on GH. Please move it to the mailing list. If an RFC is created and accepted, I can't stop you from adding it to core. But I personally don't see the point. |
The value is not present in the error message whole, if the value is longer than 15 characters. Here is the example again:
Message: It is not enough if you are working with longer values. For example, Base.com does not have ID's for delivery, they only give you the delivery name.
The value in error message would be this:
As you can see, when working with text, you would very often not get the whole value in error message.
I think you are still working with the assumption that the whole value is visible in the error message. I believe it would be more like catching a SoapFault to get the detail in addition to the error message. Again, working with the fact that the value is actually not contained (not whole) in the error message, and you have to do something extra to get it, something that you would not do with other exceptions.
Sorry, but i am not sure what GH means. |
Sorry, i just realized GH probably means GitHub. |
GitHub is not where we discuss such topics. If you feel so strongly about it, email the PHP internals mailing list. |
Yes, it's true that the value may be clipped. In that case, you are right that the message is insufficient to diagnose the error. However, this is easily solved with a var_dump or a breakpoint in your debugger. And yes, you can absolutely temporarily modify a file that doesn't belong to you. This is one of the great upsides of PHP + Composer. Even if the value was stored in the exception, you would still have to modify your code to catch and print it.
I never said stop discussing this suggestion, but discuss it on the mailing list where it belongs. PHP is community driven, and the official medium for discussions is the mailing list. GitHub is used for bug reports and simple, uncontroversial feature requests. When there are objections, they should be discussed with the community. |
Thanks for the info, i will switch to the mailing list now. |
Description
When catching a failed
match
it would be very handy if theUnhandledMatchError
had a method such asgetUnmatchedValue() : mixed
that contained the actual unmatched value.This could allow more graceful handling of failed match handlers.
This would allow as a very basic example:
I'm sure there is more involved handling you could do with it as well.
@derickr advised me to open a ticket for this idea
The text was updated successfully, but these errors were encountered: