Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f61d87

Browse files
committed
[HttpClient] Append url to JsonException messages.
1 parent 6a7ae92 commit 2f61d87

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,21 @@ public function toArray(bool $throw = true): array
147147
$contentType = $this->headers['content-type'][0] ?? 'application/json';
148148

149149
if (!preg_match('/\bjson\b/i', $contentType)) {
150-
throw new JsonException(sprintf('Response content-type is "%s" while a JSON-compatible one was expected.', $contentType));
150+
throw new JsonException(sprintf('Response content-type is "%s" while a JSON-compatible one was expected for "%s".', $contentType, $this->getInfo('url')));
151151
}
152152

153153
try {
154154
$content = json_decode($content, true, 512, JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? JSON_THROW_ON_ERROR : 0));
155155
} catch (\JsonException $e) {
156-
throw new JsonException($e->getMessage(), $e->getCode());
156+
throw new JsonException(sprintf('JSON error: %s, from "%s".', $e->getMessage(), $this->getInfo('url')), $e->getCode());
157157
}
158158

159159
if (\PHP_VERSION_ID < 70300 && JSON_ERROR_NONE !== json_last_error()) {
160-
throw new JsonException(json_last_error_msg(), json_last_error());
160+
throw new JsonException(sprintf('JSON error: %s, from "%s".', json_last_error_msg(), $this->getInfo('url')), json_last_error());
161161
}
162162

163163
if (!\is_array($content)) {
164-
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned.', \gettype($content)));
164+
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned by "%s".', \gettype($content), $this->getInfo('url')));
165165
}
166166

167167
if (null !== $this->content) {

src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,31 @@ public function toArrayErrors()
3838
yield [
3939
'content' => '{}',
4040
'responseHeaders' => ['content-type' => 'plain/text'],
41-
'message' => 'Response content-type is "plain/text" while a JSON-compatible one was expected.',
41+
'message' => 'Response content-type is "plain/text" while a JSON-compatible one was expected for "https://example.com/file.json".',
4242
];
4343

4444
yield [
4545
'content' => 'not json',
4646
'responseHeaders' => [],
47-
'message' => 'Syntax error',
47+
'message' => 'JSON error: Syntax error, from "https://example.com/file.json".',
4848
];
4949

5050
yield [
5151
'content' => '[1,2}',
5252
'responseHeaders' => [],
53-
'message' => 'State mismatch (invalid or malformed JSON)',
53+
'message' => 'JSON error: State mismatch (invalid or malformed JSON), from "https://example.com/file.json".',
5454
];
5555

5656
yield [
5757
'content' => '"not an array"',
5858
'responseHeaders' => [],
59-
'message' => 'JSON content was expected to decode to an array, string returned.',
59+
'message' => 'JSON content was expected to decode to an array, string returned by "https://example.com/file.json".',
6060
];
6161

6262
yield [
6363
'content' => '8',
6464
'responseHeaders' => [],
65-
'message' => 'JSON content was expected to decode to an array, integer returned.',
65+
'message' => 'JSON content was expected to decode to an array, integer returned by "https://example.com/file.json".',
6666
];
6767
}
6868
}

0 commit comments

Comments
 (0)