Skip to content
Draft
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
15 changes: 5 additions & 10 deletions src/Contracts/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,41 @@
namespace Meilisearch\Contracts;

use Meilisearch\Exceptions\ApiException;
use Meilisearch\Exceptions\JsonDecodingException;
use Meilisearch\Exceptions\JsonEncodingException;
use Psr\Http\Message\StreamInterface;

interface Http
{
/**
* @throws ApiException
* @throws JsonDecodingException
* @throws \JsonException
*/
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);

/**
* @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 = []);

Expand All @@ -52,5 +47,5 @@
* @throws ApiException
* @throws JsonEncodingException
*/
public function postStream(string $path, $body = null, array $query = []): StreamInterface;

Check failure on line 50 in src/Contracts/Http.php

View workflow job for this annotation

GitHub Actions / phpstan-tests

PHPDoc tag `@throws` with type Meilisearch\Contracts\JsonEncodingException|Meilisearch\Exceptions\ApiException is not subtype of Throwable
}
13 changes: 0 additions & 13 deletions src/Exceptions/JsonDecodingException.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Exceptions/JsonEncodingException.php

This file was deleted.

8 changes: 3 additions & 5 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Meilisearch\Exceptions\ApiException;
use Meilisearch\Exceptions\CommunicationException;
use Meilisearch\Exceptions\InvalidResponseBodyException;
use Meilisearch\Exceptions\JsonDecodingException;
use Meilisearch\Exceptions\JsonEncodingException;
use Meilisearch\Http\Serialize\Json;
use Meilisearch\Meilisearch;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -84,7 +82,7 @@
* @throws ApiException
* @throws ClientExceptionInterface
* @throws CommunicationException
* @throws JsonEncodingException
* @throws \JsonException
*/
public function post(string $path, $body = null, array $query = [], ?string $contentType = null)
{
Expand All @@ -105,7 +103,7 @@
* @throws ApiException
* @throws ClientExceptionInterface
* @throws CommunicationException
* @throws JsonEncodingException
* @throws \JsonException
*/
public function put(string $path, $body = null, array $query = [], ?string $contentType = null)
{
Expand Down Expand Up @@ -146,7 +144,7 @@
* @throws CommunicationException
* @throws JsonEncodingException
*/
public function postStream(string $path, $body = null, array $query = []): StreamInterface

Check failure on line 147 in src/Http/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-tests

PHPDoc tag `@throws` with type Meilisearch\Exceptions\ApiException|Meilisearch\Exceptions\CommunicationException|Meilisearch\Http\JsonEncodingException|Psr\Http\Client\ClientExceptionInterface is not subtype of Throwable
{
$request = $this->requestFactory->createRequest(
'POST',
Expand Down Expand Up @@ -199,7 +197,7 @@
if ($this->isJSONResponse($response->getHeader('content-type'))) {
try {
$body = $this->json->unserialize($bodyContent) ?? $response->getReasonPhrase();
} catch (JsonDecodingException $e) {

Check failure on line 200 in src/Http/Client.php

View workflow job for this annotation

GitHub Actions / phpstan-tests

Caught class Meilisearch\Http\JsonDecodingException not found.
$body = '' !== $bodyContent ? $bodyContent : $response->getReasonPhrase();
}
} else {
Expand Down Expand Up @@ -229,7 +227,7 @@
/**
* @throws ApiException
* @throws InvalidResponseBodyException
* @throws JsonDecodingException
* @throws \JsonException
*/
private function parseResponse(ResponseInterface $response)
{
Expand Down
22 changes: 2 additions & 20 deletions src/Http/Serialize/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,15 @@

namespace Meilisearch\Http\Serialize;

use Meilisearch\Exceptions\JsonDecodingException;
use Meilisearch\Exceptions\JsonEncodingException;

class Json implements SerializerInterface
{
private const JSON_ENCODE_ERROR_MESSAGE = 'Encoding payload to json failed: "%s".';
private const JSON_DECODE_ERROR_MESSAGE = 'Decoding payload to json failed: "%s".';

public function serialize($data)
{
try {
$encoded = json_encode($data, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonEncodingException(\sprintf(self::JSON_ENCODE_ERROR_MESSAGE, $e->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);
}
}
7 changes: 2 additions & 5 deletions src/Http/Serialize/SerializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Meilisearch\Http\Serialize;

use Meilisearch\Exceptions\JsonDecodingException;
use Meilisearch\Exceptions\JsonEncodingException;

interface SerializerInterface
{
/**
Expand All @@ -16,7 +13,7 @@ interface SerializerInterface
*
* @return string|bool
*
* @throws JsonEncodingException
* @throws \JsonException
*/
public function serialize($data);

Expand All @@ -25,7 +22,7 @@ public function serialize($data);
*
* @return string|int|float|bool|array<mixed>|null
*
* @throws JsonDecodingException
* @throws \JsonException
*/
public function unserialize(string $string);
}
5 changes: 2 additions & 3 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"];

Expand Down
26 changes: 12 additions & 14 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}");
}
Expand All @@ -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('/', '');
}
Expand Down Expand Up @@ -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}");
}
Expand All @@ -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('/', '');
}
Expand Down Expand Up @@ -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}");
}
Expand All @@ -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('/', '');
}
Expand Down
10 changes: 4 additions & 6 deletions tests/Http/Serialize/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}

Expand All @@ -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));
}
}
Loading