This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpClient/Exception
Fabien Potencier 041f60f80a 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 https://github.com/api-platform/core/pull/2608#issuecomment-472510732.

Commits
-------

96df4464a1 [HttpClient] Parse common API error formats for better exception messages
2019-03-24 18:15:37 +01:00
..
ClientException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00
HttpExceptionTrait.php feature #30559 [HttpClient] Parse common API error formats for better exception messages (dunglas) 2019-03-24 18:15:37 +01:00
InvalidArgumentException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00
JsonException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00
RedirectionException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00
ServerException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00
TransportException.php [HttpClient] Make exceptions public 2019-03-17 08:23:49 +01:00