Skip to content

[HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property #38122

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

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function __construct(ResponseInterface $response)
// see http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors
$separator = isset($body['hydra:title'], $body['hydra:description']) ? "\n\n" : '';
$message = ($body['hydra:title'] ?? '').$separator.($body['hydra:description'] ?? '');
} elseif (isset($body['title']) || isset($body['detail'])) {
} elseif ((isset($body['title']) || isset($body['detail']))
&& (is_scalar($body['title'] ?? '') && is_scalar($body['detail'] ?? ''))) {
// see RFC 7807 and https://jsonapi.org/format/#error-objects
$separator = isset($body['title'], $body['detail']) ? "\n\n" : '';
$message = ($body['title'] ?? '').$separator.($body['detail'] ?? '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ class HttpExceptionTraitTest extends TestCase
{
public function provideParseError(): iterable
{
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}'];
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}'];
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}'];
$errorWithoutMessage = 'HTTP/1.1 400 Bad Request returned for "http://example.com".';

$errorWithMessage = <<<ERROR
An error occurred

Some details
ERROR;

yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}', $errorWithMessage];
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
yield ['application/json', '{"title": "An error occurred", "detail": {"field_name": ["Some details"]}}', $errorWithoutMessage];
}

/**
* @dataProvider provideParseError
*/
public function testParseError(string $mimeType, string $json): void
public function testParseError(string $mimeType, string $json, string $expectedMessage): void
{
$response = $this->createMock(ResponseInterface::class);
$response
Expand All @@ -47,12 +56,7 @@ public function testParseError(string $mimeType, string $json): void

$e = new TestException($response);
$this->assertSame(400, $e->getCode());
$this->assertSame(<<<ERROR
An error occurred

Some details
ERROR
, $e->getMessage());
$this->assertSame($expectedMessage, $e->getMessage());
}
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy