Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromdunglas:json-errors
Mar 24, 2019

Conversation

@dunglas
Copy link
Member

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRtodo?

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'sConstraintViolationListNormalizer 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):

publicfunctiontestBadRequest()    {$this->expectException(ClientExceptionInterface::class);$this->expectExceptionCode(400);// HTTP status code$this->expectExceptionMessage(<<<ERRORValidation Failedusers: 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 ofapi-platform/core#2608 (comment).

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

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.

@dunglas
Copy link
MemberAuthor

@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

@fabpot
Copy link
Member

Thank you@dunglas.

@fabpotfabpot merged commit96df446 intosymfony:masterMar 24, 2019
fabpot added a commit that referenced this pull requestMar 24, 2019
… 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     <!-- seehttps://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(<<<ERRORValidation Failedusers: 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 ofapi-platform/core#2608 (comment).Commits-------96df446 [HttpClient] Parse common API error formats for better exception messages
@nicolas-grekasnicolas-grekas modified the milestones:next,4.3Apr 30, 2019
@fabpotfabpot mentioned this pull requestMay 9, 2019
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@javiereguiluzjaviereguiluzjaviereguiluz left review comments

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

5 participants

@dunglas@fabpot@javiereguiluz@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp