Skip to content

Commit 81a8024

Browse files
Striftnorkunas
andcommitted
Remove custom JSON exceptions (#759)
* Remove custom JSON exceptions * Lint * Update src/Http/Serialize/Json.php Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com> --------- Co-authored-by: Tomas Norkūnas <norkunas.tom@gmail.com>
1 parent 8def4bd commit 81a8024

File tree

9 files changed

+30
-89
lines changed

9 files changed

+30
-89
lines changed

src/Contracts/Http.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,41 @@
55
namespace Meilisearch\Contracts;
66

77
use Meilisearch\Exceptions\ApiException;
8-
use Meilisearch\Exceptions\JsonDecodingException;
9-
use Meilisearch\Exceptions\JsonEncodingException;
108
use Psr\Http\Message\StreamInterface;
119

1210
interface Http
1311
{
1412
/**
1513
* @throws ApiException
16-
* @throws JsonDecodingException
14+
* @throws \JsonException
1715
*/
1816
public function get(string $path, array $query = []);
1917

2018
/**
2119
* @param non-empty-string|null $contentType
2220
*
2321
* @throws ApiException
24-
* @throws JsonEncodingException
25-
* @throws JsonDecodingException
22+
* @throws \JsonException
2623
*/
2724
public function post(string $path, $body = null, array $query = [], ?string $contentType = null);
2825

2926
/**
3027
* @param non-empty-string|null $contentType
3128
*
3229
* @throws ApiException
33-
* @throws JsonEncodingException
34-
* @throws JsonDecodingException
30+
* @throws \JsonException
3531
*/
3632
public function put(string $path, $body = null, array $query = [], ?string $contentType = null);
3733

3834
/**
3935
* @throws ApiException
40-
* @throws JsonEncodingException
41-
* @throws JsonDecodingException
36+
* @throws \JsonException
4237
*/
4338
public function patch(string $path, $body = null, array $query = []);
4439

4540
/**
4641
* @throws ApiException
47-
* @throws JsonDecodingException
42+
* @throws \JsonException
4843
*/
4944
public function delete(string $path, array $query = []);
5045

src/Exceptions/JsonDecodingException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Exceptions/JsonEncodingException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Http/Client.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Meilisearch\Exceptions\ApiException;
1111
use Meilisearch\Exceptions\CommunicationException;
1212
use Meilisearch\Exceptions\InvalidResponseBodyException;
13-
use Meilisearch\Exceptions\JsonDecodingException;
14-
use Meilisearch\Exceptions\JsonEncodingException;
1513
use Meilisearch\Http\Serialize\Json;
1614
use Meilisearch\Meilisearch;
1715
use Psr\Http\Client\ClientExceptionInterface;
@@ -84,7 +82,7 @@ public function get(string $path, array $query = [])
8482
* @throws ApiException
8583
* @throws ClientExceptionInterface
8684
* @throws CommunicationException
87-
* @throws JsonEncodingException
85+
* @throws \JsonException
8886
*/
8987
public function post(string $path, $body = null, array $query = [], ?string $contentType = null)
9088
{
@@ -105,7 +103,7 @@ public function post(string $path, $body = null, array $query = [], ?string $con
105103
* @throws ApiException
106104
* @throws ClientExceptionInterface
107105
* @throws CommunicationException
108-
* @throws JsonEncodingException
106+
* @throws \JsonException
109107
*/
110108
public function put(string $path, $body = null, array $query = [], ?string $contentType = null)
111109
{
@@ -232,7 +230,7 @@ private function buildQueryString(array $queryParams = []): string
232230
/**
233231
* @throws ApiException
234232
* @throws InvalidResponseBodyException
235-
* @throws JsonDecodingException
233+
* @throws \JsonException
236234
*/
237235
private function parseResponse(ResponseInterface $response)
238236
{

src/Http/Serialize/Json.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,15 @@
44

55
namespace Meilisearch\Http\Serialize;
66

7-
use Meilisearch\Exceptions\JsonDecodingException;
8-
use Meilisearch\Exceptions\JsonEncodingException;
9-
107
class Json implements SerializerInterface
118
{
12-
private const JSON_ENCODE_ERROR_MESSAGE = 'Encoding payload to json failed: "%s".';
13-
private const JSON_DECODE_ERROR_MESSAGE = 'Decoding payload to json failed: "%s".';
14-
159
public function serialize($data)
1610
{
17-
try {
18-
$encoded = json_encode($data, JSON_THROW_ON_ERROR);
19-
} catch (\JsonException $e) {
20-
throw new JsonEncodingException(\sprintf(self::JSON_ENCODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
21-
}
22-
23-
return $encoded;
11+
return json_encode($data, JSON_THROW_ON_ERROR);
2412
}
2513

2614
public function unserialize(string $string)
2715
{
28-
try {
29-
$decoded = json_decode($string, true, 512, JSON_THROW_ON_ERROR);
30-
} catch (\JsonException $e) {
31-
throw new JsonDecodingException(\sprintf(self::JSON_DECODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
32-
}
33-
34-
return $decoded;
16+
return json_decode($string, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
3517
}
3618
}

src/Http/Serialize/SerializerInterface.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Meilisearch\Http\Serialize;
66

7-
use Meilisearch\Exceptions\JsonDecodingException;
8-
use Meilisearch\Exceptions\JsonEncodingException;
9-
107
interface SerializerInterface
118
{
129
/**
@@ -16,7 +13,7 @@ interface SerializerInterface
1613
*
1714
* @return string|bool
1815
*
19-
* @throws JsonEncodingException
16+
* @throws \JsonException
2017
*/
2118
public function serialize($data);
2219

@@ -25,7 +22,7 @@ public function serialize($data);
2522
*
2623
* @return string|int|float|bool|array<mixed>|null
2724
*
28-
* @throws JsonDecodingException
25+
* @throws \JsonException
2926
*/
3027
public function unserialize(string $string);
3128
}

tests/Endpoints/DocumentsTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Meilisearch\Exceptions\ApiException;
1111
use Meilisearch\Exceptions\InvalidArgumentException;
1212
use Meilisearch\Exceptions\InvalidResponseBodyException;
13-
use Meilisearch\Exceptions\JsonEncodingException;
1413
use Meilisearch\Http\Client;
1514
use Psr\Http\Message\ResponseInterface;
1615
use Tests\TestCase;
@@ -153,8 +152,8 @@ public function testAddDocumentsNdJson(): void
153152

154153
public function testCannotAddDocumentWhenJsonEncodingFails(): void
155154
{
156-
$this->expectException(JsonEncodingException::class);
157-
$this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".');
155+
$this->expectException(\JsonException::class);
156+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
158157

159158
$documents = ["\xB1\x31"];
160159

tests/Http/ClientTest.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use Meilisearch\Exceptions\ApiException;
88
use Meilisearch\Exceptions\InvalidResponseBodyException;
9-
use Meilisearch\Exceptions\JsonDecodingException;
10-
use Meilisearch\Exceptions\JsonEncodingException;
119
use Meilisearch\Http\Client;
1210
use Meilisearch\Meilisearch;
1311
use PHPUnit\Framework\MockObject\MockObject;
@@ -57,8 +55,8 @@ public function testPostThrowsWithInvalidBody(): void
5755
{
5856
$client = new Client('https://localhost');
5957

60-
$this->expectException(JsonEncodingException::class);
61-
$this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".');
58+
$this->expectException(\JsonException::class);
59+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
6260

6361
$client->post('/', "{'Bad JSON':\xB1\x31}");
6462
}
@@ -72,8 +70,8 @@ public function testPostThrowsWithInvalidResponse(int $statusCode): void
7270

7371
$client = new Client('https://localhost', null, $httpClient);
7472

75-
$this->expectException(JsonDecodingException::class);
76-
$this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"');
73+
$this->expectException(\JsonException::class);
74+
$this->expectExceptionMessage('Syntax error');
7775

7876
$client->post('/', '');
7977
}
@@ -104,8 +102,8 @@ public function testPutThrowsWithInvalidBody(): void
104102
{
105103
$client = new Client('https://localhost');
106104

107-
$this->expectException(JsonEncodingException::class);
108-
$this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".');
105+
$this->expectException(\JsonException::class);
106+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
109107

110108
$client->put('/', "{'Bad JSON':\xB1\x31}");
111109
}
@@ -119,8 +117,8 @@ public function testPutThrowsWithInvalidResponse(int $statusCode): void
119117

120118
$client = new Client('https://localhost', null, $httpClient);
121119

122-
$this->expectException(JsonDecodingException::class);
123-
$this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"');
120+
$this->expectException(\JsonException::class);
121+
$this->expectExceptionMessage('Syntax error');
124122

125123
$client->put('/', '');
126124
}
@@ -151,8 +149,8 @@ public function testPatchThrowsWithInvalidBody(): void
151149
{
152150
$client = new Client('https://localhost');
153151

154-
$this->expectException(JsonEncodingException::class);
155-
$this->expectExceptionMessage('Encoding payload to json failed: "Malformed UTF-8 characters, possibly incorrectly encoded".');
152+
$this->expectException(\JsonException::class);
153+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
156154

157155
$client->patch('/', "{'Bad JSON':\xB1\x31}");
158156
}
@@ -166,8 +164,8 @@ public function testPatchThrowsWithInvalidResponse(int $statusCode): void
166164

167165
$client = new Client('https://localhost', null, $httpClient);
168166

169-
$this->expectException(JsonDecodingException::class);
170-
$this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"');
167+
$this->expectException(\JsonException::class);
168+
$this->expectExceptionMessage('Syntax error');
171169

172170
$client->put('/', '');
173171
}

tests/Http/Serialize/JsonTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Tests\Http\Serialize;
66

7-
use Meilisearch\Exceptions\JsonDecodingException;
8-
use Meilisearch\Exceptions\JsonEncodingException;
97
use Meilisearch\Http\Serialize\Json;
108
use PHPUnit\Framework\TestCase;
119

@@ -22,8 +20,8 @@ public function testSerializeWithInvalidData(): void
2220
{
2321
$data = ['id' => NAN, 'title' => NAN];
2422
$json = new Json();
25-
$this->expectException(JsonEncodingException::class);
26-
$this->expectExceptionMessage('Encoding payload to json failed: "Inf and NaN cannot be JSON encoded".');
23+
$this->expectException(\JsonException::class);
24+
$this->expectExceptionMessage('Inf and NaN cannot be JSON encoded');
2725
self::assertSame(json_encode($data), $json->serialize($data));
2826
}
2927

@@ -38,8 +36,8 @@ public function testUnserializeWithInvalidData(): void
3836
{
3937
$data = "{'id':287947,'title':'\xB1\x31'}";
4038
$json = new Json();
41-
$this->expectException(JsonDecodingException::class);
42-
$this->expectExceptionMessage('Decoding payload to json failed: "Syntax error"');
39+
$this->expectException(\JsonException::class);
40+
$this->expectExceptionMessage('Syntax error');
4341
self::assertSame(['id' => 287947, 'title' => 'Some ID'], $json->unserialize($data));
4442
}
4543
}

0 commit comments

Comments
 (0)