Skip to content

[HttpClient] Add response status code helper #60605

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

jimmy-martin
Copy link

@jimmy-martin jimmy-martin commented May 31, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

This PR adds a set of expressive helper methods to simplify HTTP status code checks on ResponseInterface objects that use CommonResponseTrait trait.

These new methods allow developers to write more readable and intention-revealing code when working with HttpClient.

Before:

// opt-out from exception to manually check
$data = $response->toArray(false);

if (404 === $response->getStatusCode()) {
    // not found
}

After:

// opt-out from exception to manually check
$data = $response->toArray(false);

if ($response->isNotFound()) {
    // not found
}

I've intentionally selected the most common status codes to keep this initial set focused and practical.

That said, I'm open to feedback on:

  • the naming of the trait (ResponseStatusCodeTrait)
  • whether we should add more methods (e.g., 418 😉) or remove less useful ones
  • alternative implementation styles, if more in line with Symfony conventions.

PS: This is my 1st PR to Symfony.

@carsonbot

This comment has been minimized.

@jimmy-martin jimmy-martin force-pushed the feature/http-client-add-response-status-code-helper branch from ed1ebac to bf2eae5 Compare June 1, 2025 19:34
@antonkomarev
Copy link

Maybe it's not the Symfony way, but a bit of another proposal.

// opt-out from exception to manually check
$data = $response->toArray(false);

if ($response->status()->isNotFound()) {
    // not found
}

@jimmy-martin
Copy link
Author

jimmy-martin commented Jun 2, 2025

Maybe it's not the Symfony way, but a bit of another proposal.

// opt-out from exception to manually check
$data = $response->toArray(false);

if ($response->status()->isNotFound()) {
    // not found
}

Let's see what others think. The implementation will be different in this case. I think this way is simpler and more straightforward.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 2, 2025

I understand the motivation for this PR, but it has a major drawback: the added methods are not discoverable : since the methods are not in ResponseInterface, no IDE nor static analyzer will be able to know about them - and no human reader either.

It will work "by chance", since no base contracts will ensure these methods are actually present on returned response objects.

We could add them on ResponseInterface, but I'd be against it: HTTP status codes are even more standard stuff than any abstraction, so we recommend using them directly instead.

If you really want that experience, I'd suggest using a static class with helpers instead:

HttpStatus::isForbidden($response->getStatusCode())

(I'm not suggesting to add this helper to Symfony either ;))

Closing therefor. Thanks for your understanding and thanks for proposing!

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.

4 participants