[9.x] Add new throw helper methods to the HTTP Client - #45704
Conversation
…ific status code is returned from the HTTP response
|
This type of helper (alias) methods are making the framework bulky and increasing learning curve. $response->throwIf($callback) is already sufficient. |
…rowUnlessStatus method
|
I managed to work on a simpler approach for the implementation that I wanted. @ankurk91 I understand your point, but the methods I'm adding are to decrease work for the developers. It's a syntax sugar to make it easier to throw exceptions based on the response HTTP status code. |
|
If this has any chances to be approved and merged and additional work is needed for docs or something like that, just let me know that I'll be happy to work on the needed updates as well 😉 💪🏼 |
|
@ankurk91 I think that I was not clear about my explanation for this implementation. I'll try to explain it a little better: The These methods I added are for only throwing an exception when the HTTP response code is the one passed to the So they are for different purposes than the |
|
Thanks, you gave me a reason. |
|
What if you want to throw if the status is any 500 level status code? |
|
@taylorotwell we can do it on two ways. Adding additional parameters to the methods I created or create two other ones called |
|
@taylorotwell I added a new commit adding the two methods I mentioned:
If you feel like it's needed I can add four more methods:
If you think that's not needed or that I can work on those other four methods on a new PR it's all good for me. I'll be happy to help in all cases. 😉 💪🏼 |
|
Added support for callables: $this->factory->get('http://foo.com/api/400')->throwIfStatus(fn ($status) => $status === 500); |
@taylorotwell amazing addition, I didn't think of this at first! Thanks for all the help on getting this merged! 💪🏼 |
The goal is to add four new methods to the HTTP Client:
Sometimes we want to only throw an exception in our application for specific codes of the response, so these method add an easy way to accomplish that.
The
throwIfmethod only throws the exception if two conditions aretrue: The condition passed resolves totrueAND if the HTTP response code is>= 400