Skip to content

Commit 3b08ef4

Browse files
committed
parse error message for json improvement
1 parent 00efe50 commit 3b08ef4

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require-dev": {
2424
"ext-pdo": "*",
2525
"phpstan/phpstan": "^1.2",
26-
"phpunit/phpunit": "^9.5|^10.0",
26+
"phpunit/phpunit": "^10.0",
2727
"phpstan/phpstan-strict-rules": "^1.1",
2828
"phpstan/phpstan-deprecation-rules": "^1.0"
2929
},

src/Service/Http/MessageService.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ public static function parseJsonFromBody(
2222
bool $allowInvalidJson,
2323
int $jsonDecodeFlags = 0
2424
): Rules {
25-
$content = $message->getBody()->getContents();
26-
$content = \json_decode($content, flags: $jsonDecodeFlags);
25+
$contentRaw = $message->getBody()->getContents();
26+
$content = \json_decode($contentRaw, flags: $jsonDecodeFlags);
2727
if (\json_last_error() !== JSON_ERROR_NONE) {
2828
if ($allowInvalidJson) {
2929
$content = null;
3030
} else {
31-
throw $exceptionFactory->create($messageBodyName.' must be valid json');
31+
$truncated = strlen($contentRaw) > 200;
32+
$contentRaw = substr($contentRaw, 0, 200);
33+
throw $exceptionFactory->create($messageBodyName.' must be valid json, invalid content: \''.$contentRaw.'\''.($truncated ? ' (truncated)' : ''));
3234
}
3335
}
3436

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use GuzzleHttp\Psr7\HttpFactory;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleAsFuck\ApiToolkit\Service\Http\MessageService;
10+
use SimpleAsFuck\Validator\Factory\UnexpectedValueException;
11+
12+
#[CoversClass(MessageService::class)]
13+
final class MessageServiceTest extends TestCase
14+
{
15+
private HttpFactory $httpFactory;
16+
17+
protected function setUp(): void
18+
{
19+
$this->httpFactory = new HttpFactory();
20+
}
21+
22+
#[DataProvider('dataParseErrorMessage')]
23+
public function testParseErrorMessage(string $expectedErrorMessage, string $invalidContent): void
24+
{
25+
$this->expectExceptionMessage($expectedErrorMessage);
26+
27+
$message = $this->httpFactory->createResponse()->withBody($this->httpFactory->createStream($invalidContent));
28+
MessageService::parseJsonFromBody(new UnexpectedValueException(), $message, 'Test body', false);
29+
}
30+
31+
/**
32+
* @return non-empty-array<non-empty-array<string>>
33+
*/
34+
public static function dataParseErrorMessage(): array
35+
{
36+
return [
37+
['Test body must be valid json, invalid content: \'\'', ''],
38+
['Test body must be valid json, invalid content: \'kjdfhgroigiosdiugaeiufsabdv\'', 'kjdfhgroigiosdiugaeiufsabdv'],
39+
['Test body must be valid json, invalid content: \'kjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigi\' (truncated)', 'kjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdvkjdfhgroigiosdiugaeiufsabdv'],
40+
];
41+
}
42+
}

0 commit comments

Comments
 (0)