-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Parse common API error formats for better exception messages #30559
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
d2477c7
to
4380807
Compare
src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php
Outdated
Show resolved
Hide resolved
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.
Nice! Just one thing: we should skip this logic when $this instanceof RedirectionExceptionInterface
I think.
@nicolas-grekas I would keep it even for 300 exceptions because Hydra allows to provide better messages even for such status codes, and it doesn't hurt: http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors |
Thank you @dunglas. |
… exception messages (dunglas) This PR was squashed before being merged into the 4.3-dev branch (closes #30559). Discussion ---------- [HttpClient] Parse common API error formats for better exception messages | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | todo? Use extra details provided by popular error formats following to improve HTTP exception messages. The following formats are supported: * Hydra (default in API Platform) * RFC 7807 (followed by Symfony's [ConstraintViolationListNormalizer](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php) and supported by API Platform and Apigility) * JSON:API (because it respects the semantic of the RFC 7807) It allows to write code like the following (here in a test context): ```php public function testBadRequest() { $this->expectException(ClientExceptionInterface::class); $this->expectExceptionCode(400); // HTTP status code $this->expectExceptionMessage(<<<ERROR Validation Failed users: This collection should contain 1 element or more. users: The current logged in user must be part of the users owning this resource. ERROR ); $response = (HttpClient::create())->request('POST', 'http://example.com/api/projects', [ 'json' => [ 'name' => 'My project', ], ]); $response->getContent(); } ``` Port of api-platform/core#2608 (comment). Commits ------- 96df446 [HttpClient] Parse common API error formats for better exception messages
Use extra details provided by popular error formats following to improve HTTP exception messages.
The following formats are supported:
It allows to write code like the following (here in a test context):
Port of api-platform/core#2608 (comment).