Skip to content

Commit 8fb29d8

Browse files
committed
Catch json errors the > PHP 7.3 way
1 parent d070e26 commit 8fb29d8

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/MinecraftQueryResolver.php

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

55
namespace PHPMinecraft\MinecraftQuery;
66

7-
use JsonException;
8-
97
class MinecraftQueryResolver
108
{
119
/** @var string */
@@ -122,10 +120,14 @@ public function retrieveData(): void
122120
throw new MinecraftQueryException('Server did not return any data');
123121
}
124122

125-
try {
126-
$this->rawData = (array) json_decode($jsonData, true, 512, JSON_THROW_ON_ERROR);
127-
} catch (JsonException $e) {
128-
throw new MinecraftQueryException(sprintf('JsonException, server sent invalid json (%s)', $e->getMessage()));
123+
$this->rawData = (array) json_decode($jsonData, true);
124+
125+
if (json_last_error() !== JSON_ERROR_NONE) {
126+
if (function_exists('json_last_error_msg')) {
127+
throw new MinecraftQueryException(json_last_error_msg());
128+
} else {
129+
throw new MinecraftQueryException( 'JsonException, server sent invalid json');
130+
}
129131
}
130132

131133
$this->rawData['latency'] = (int) ($timeEnd * 1000 - $timeStart * 1000);

0 commit comments

Comments
 (0)