diff --git a/src/Contracts/Http.php b/src/Contracts/Http.php index d20d0e64..6f46689e 100644 --- a/src/Contracts/Http.php +++ b/src/Contracts/Http.php @@ -5,14 +5,12 @@ namespace Meilisearch\Contracts; use Meilisearch\Exceptions\ApiException; -use Meilisearch\Exceptions\JsonDecodingException; -use Meilisearch\Exceptions\JsonEncodingException; interface Http { /** * @throws ApiException - * @throws JsonDecodingException + * @throws \JsonException */ public function get(string $path, array $query = []); @@ -20,8 +18,7 @@ public function get(string $path, array $query = []); * @param non-empty-string|null $contentType * * @throws ApiException - * @throws JsonEncodingException - * @throws JsonDecodingException + * @throws \JsonException */ public function post(string $path, $body = null, array $query = [], ?string $contentType = null); @@ -29,21 +26,19 @@ public function post(string $path, $body = null, array $query = [], ?string $con * @param non-empty-string|null $contentType * * @throws ApiException - * @throws JsonEncodingException - * @throws JsonDecodingException + * @throws \JsonException */ public function put(string $path, $body = null, array $query = [], ?string $contentType = null); /** * @throws ApiException - * @throws JsonEncodingException - * @throws JsonDecodingException + * @throws \JsonException */ public function patch(string $path, $body = null, array $query = []); /** * @throws ApiException - * @throws JsonDecodingException + * @throws \JsonException */ public function delete(string $path, array $query = []); } diff --git a/src/Exceptions/JsonDecodingException.php b/src/Exceptions/JsonDecodingException.php deleted file mode 100644 index 6f5f5229..00000000 --- a/src/Exceptions/JsonDecodingException.php +++ /dev/null @@ -1,13 +0,0 @@ -getMessage()), $e->getCode(), $e); - } - - return $encoded; + return json_encode($data, JSON_THROW_ON_ERROR); } public function unserialize(string $string) { - try { - $decoded = json_decode($string, true, 512, JSON_THROW_ON_ERROR); - } catch (\JsonException $e) { - throw new JsonDecodingException(\sprintf(self::JSON_DECODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e); - } - - return $decoded; + return json_decode($string, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); } } diff --git a/src/Http/Serialize/SerializerInterface.php b/src/Http/Serialize/SerializerInterface.php index c39e1fab..8e076019 100644 --- a/src/Http/Serialize/SerializerInterface.php +++ b/src/Http/Serialize/SerializerInterface.php @@ -4,9 +4,6 @@ namespace Meilisearch\Http\Serialize; -use Meilisearch\Exceptions\JsonDecodingException; -use Meilisearch\Exceptions\JsonEncodingException; - interface SerializerInterface { /** @@ -16,7 +13,7 @@ interface SerializerInterface * * @return string|bool * - * @throws JsonEncodingException + * @throws \JsonException */ public function serialize($data); @@ -25,7 +22,7 @@ public function serialize($data); * * @return string|int|float|bool|array|null * - * @throws JsonDecodingException + * @throws \JsonException */ public function unserialize(string $string); } diff --git a/tests/Endpoints/DocumentsTest.php b/tests/Endpoints/DocumentsTest.php index 720e7400..5df160a9 100644 --- a/tests/Endpoints/DocumentsTest.php +++ b/tests/Endpoints/DocumentsTest.php @@ -10,7 +10,6 @@ use Meilisearch\Exceptions\ApiException; use Meilisearch\Exceptions\InvalidArgumentException; use Meilisearch\Exceptions\InvalidResponseBodyException; -use Meilisearch\Exceptions\JsonEncodingException; use Meilisearch\Http\Client; use Psr\Http\Message\ResponseInterface; use Tests\TestCase; @@ -153,8 +152,8 @@ public function testAddDocumentsNdJson(): void public function testCannotAddDocumentWhenJsonEncodingFails(): void { - $this->expectException(JsonEncodingException::class); - $this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); $documents = ["\xB1\x31"]; diff --git a/tests/Http/ClientTest.php b/tests/Http/ClientTest.php index 0e53b220..8e72cabb 100644 --- a/tests/Http/ClientTest.php +++ b/tests/Http/ClientTest.php @@ -6,8 +6,6 @@ use Meilisearch\Exceptions\ApiException; use Meilisearch\Exceptions\InvalidResponseBodyException; -use Meilisearch\Exceptions\JsonDecodingException; -use Meilisearch\Exceptions\JsonEncodingException; use Meilisearch\Http\Client; use Meilisearch\Meilisearch; use PHPUnit\Framework\MockObject\MockObject; @@ -57,8 +55,8 @@ public function testPostThrowsWithInvalidBody(): void { $client = new Client('https://localhost'); - $this->expectException(JsonEncodingException::class); - $this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); $client->post('/', "{'Bad JSON':\xB1\x31}"); } @@ -72,8 +70,8 @@ public function testPostThrowsWithInvalidResponse(int $statusCode): void $client = new Client('https://localhost', null, $httpClient); - $this->expectException(JsonDecodingException::class); - $this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Syntax error'); $client->post('/', ''); } @@ -104,8 +102,8 @@ public function testPutThrowsWithInvalidBody(): void { $client = new Client('https://localhost'); - $this->expectException(JsonEncodingException::class); - $this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); $client->put('/', "{'Bad JSON':\xB1\x31}"); } @@ -119,8 +117,8 @@ public function testPutThrowsWithInvalidResponse(int $statusCode): void $client = new Client('https://localhost', null, $httpClient); - $this->expectException(JsonDecodingException::class); - $this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Syntax error'); $client->put('/', ''); } @@ -151,8 +149,8 @@ public function testPatchThrowsWithInvalidBody(): void { $client = new Client('https://localhost'); - $this->expectException(JsonEncodingException::class); - $this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); $client->patch('/', "{'Bad JSON':\xB1\x31}"); } @@ -166,8 +164,8 @@ public function testPatchThrowsWithInvalidResponse(int $statusCode): void $client = new Client('https://localhost', null, $httpClient); - $this->expectException(JsonDecodingException::class); - $this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Syntax error'); $client->put('/', ''); } diff --git a/tests/Http/Serialize/JsonTest.php b/tests/Http/Serialize/JsonTest.php index b0fde989..e7a7b858 100644 --- a/tests/Http/Serialize/JsonTest.php +++ b/tests/Http/Serialize/JsonTest.php @@ -4,8 +4,6 @@ namespace Tests\Http\Serialize; -use Meilisearch\Exceptions\JsonDecodingException; -use Meilisearch\Exceptions\JsonEncodingException; use Meilisearch\Http\Serialize\Json; use PHPUnit\Framework\TestCase; @@ -22,8 +20,8 @@ public function testSerializeWithInvalidData(): void { $data = ['id' => NAN, 'title' => NAN]; $json = new Json(); - $this->expectException(JsonEncodingException::class); - $this->expectExceptionMessage('Encoding payload to json failed: "Inf and NaN cannot be JSON encoded".'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Inf and NaN cannot be JSON encoded'); self::assertSame(json_encode($data), $json->serialize($data)); } @@ -38,8 +36,8 @@ public function testUnserializeWithInvalidData(): void { $data = "{'id':287947,'title':'\xB1\x31'}"; $json = new Json(); - $this->expectException(JsonDecodingException::class); - $this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"'); + $this->expectException(\JsonException::class); + $this->expectExceptionMessage('Syntax error'); self::assertSame(['id' => 287947, 'title' => 'Some ID'], $json->unserialize($data)); } }