Skip to content
The HttpClient component provides powerful methods to fetch HTTP resources synchronously or asynchronously.
Branch: master
Clone or download
fabpot feature #30559 [HttpClient] Parse common API error formats for better…
… 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
-------

96df4464a1 [HttpClient] Parse common API error formats for better exception messages
Latest commit f97a0da Mar 24, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Chunk
Exception feature #30559 [HttpClient] Parse common API error formats for better… Mar 24, 2019
Response [HttpClient] added CachingHttpClient Mar 22, 2019
Tests
CHANGELOG.md
CachingHttpClient.php
CurlHttpClient.php [HttpClient] changes minimal php version to use curl push function Mar 18, 2019
HttpClient.php [HttpClient] introduce the component Mar 7, 2019
HttpClientTrait.php
HttpOptions.php
LICENSE [HttpClient] introduce the component Mar 7, 2019
MockHttpClient.php [HttpClient] add MockHttpClient Mar 19, 2019
NativeHttpClient.php [FrameworkBundle] Add integration of http-client component Mar 17, 2019
Psr18Client.php [HttpClient] Add Psr18Client - aka a PSR-18 adapter Mar 7, 2019
README.md Merge branch '4.2' Mar 12, 2019
ScopingHttpClient.php
composer.json
phpunit.xml.dist [HttpClient] introduce the component Mar 7, 2019

README.md

You can’t perform that action at this time.