Skip to content
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

[HttpClient] Parse common API error formats for better exception messages #30559

Open
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
4 participants
@dunglas
Copy link
Member

dunglas commented Mar 13, 2019

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
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 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):

    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).

@dunglas dunglas requested a review from nicolas-grekas Mar 13, 2019

@dunglas dunglas force-pushed the dunglas:json-errors branch 2 times, most recently from d2477c7 to 4380807 Mar 13, 2019

Show resolved Hide resolved src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php Outdated
Show resolved Hide resolved src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php Outdated
@@ -26,10 +26,41 @@ public function __construct(ResponseInterface $response)
$url = $response->getInfo('url');
$message = sprintf('HTTP %d returned for URL "%s".', $code, $url);
$httpCodeFound = false;

This comment has been minimized.

@javiereguiluz

javiereguiluz Mar 14, 2019

Member

$httpCodeFound -> $isHttpCode or $hasHttpCode ?

This comment has been minimized.

@dunglas

dunglas Mar 14, 2019

Author Member

It always has one, the question is it already found in the array or not? (same here, I can change if it matters)

@nicolas-grekas nicolas-grekas added this to the next milestone Mar 14, 2019

dunglas added some commits Mar 13, 2019

@dunglas dunglas force-pushed the dunglas:json-errors branch from 4380807 to 90d602c Mar 14, 2019

@nicolas-grekas
Copy link
Member

nicolas-grekas left a comment

Nice! Just one thing: we should skip this logic when $this instanceof RedirectionExceptionInterface I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.