Skip to content

Commit

Permalink
Use UnexpectedValueException instead of InvalidArgumentException
Browse files Browse the repository at this point in the history
As suggested by @MGatner 🥰
  • Loading branch information
jeromegamez committed Feb 11, 2022
1 parent 24d357e commit 7102e98
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A simple helper to decode and encode JSON, including from files.

```php
use Beste\Json;
use InvalidArgumentException;
use UnexpectedValueException;

$object = Json::decode('{"key": "value"}');

Expand All @@ -25,10 +25,10 @@ $json = Json::encode($object);
$prettyJson = Json::pretty($object);

// When something goes wring while decoding/encoding,
// an `InvalidArgumentException` is thrown
// an `UnexpectedValueException` is thrown
try {
Json::decode('{]');
} catch (InvalidArgumentException $e) {
} catch (UnexpectedValueException $e) {
// Handle error
}
```
Expand Down
16 changes: 8 additions & 8 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Beste;

use InvalidArgumentException;
use JsonException;
use UnexpectedValueException;

final class Json
{
Expand All @@ -14,7 +14,7 @@ final class Json
private const DECODE_DEFAULT = JSON_BIGINT_AS_STRING;

/**
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*
* @return mixed
*/
Expand All @@ -26,19 +26,19 @@ public static function decode(string $json, ?bool $forceArray = null)
try {
return json_decode($json, $forceArray, 512, $flags | self::DECODE_DEFAULT | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new InvalidArgumentException($e->getMessage());
throw new UnexpectedValueException($e->getMessage());
}
}

/**
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*
* @return mixed
*/
public static function decodeFile(string $path, bool $forceArray = null)
{
if (!is_readable($path)) {
throw new InvalidArgumentException("The file at '$path' does not exist");
throw new UnexpectedValueException("The file at '$path' does not exist");
}

return self::decode((string) file_get_contents($path), $forceArray);
Expand All @@ -47,7 +47,7 @@ public static function decodeFile(string $path, bool $forceArray = null)
/**
* @param mixed $data
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public static function encode($data, ?int $options = null): string
{
Expand All @@ -56,14 +56,14 @@ public static function encode($data, ?int $options = null): string
try {
return json_encode($data, $options | self::ENCODE_DEFAULT | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new InvalidArgumentException($e->getMessage());
throw new UnexpectedValueException($e->getMessage());
}
}

/**
* @param mixed $value
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public static function pretty($value, ?int $options = null): string
{
Expand Down
8 changes: 4 additions & 4 deletions tests/DecodeJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Beste\Json\Tests;

use Beste\Json;
use InvalidArgumentException;
use UnexpectedValueException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -16,7 +16,7 @@ class DecodeJsonTest extends TestCase
/** @test */
public function it_rejects_invalid_json(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(UnexpectedValueException::class);

Json::decode('{');
}
Expand Down Expand Up @@ -58,7 +58,7 @@ public function it_rejects_an_unreadable_file(): void
{
$path = __DIR__.'/non-existing.json';

$this->expectException(InvalidArgumentException::class);
$this->expectException(UnexpectedValueException::class);
assert(!file_exists($path));

Json::decodeFile($path);
Expand All @@ -69,7 +69,7 @@ public function it_rejects_a_file_with_invalid_json(): void
{
$path = __DIR__.'/invalid.json';

$this->expectException(InvalidArgumentException::class);
$this->expectException(UnexpectedValueException::class);
assert(file_exists($path));

$this->assertIsObject(Json::decodeFile($path));
Expand Down
4 changes: 2 additions & 2 deletions tests/EncodeJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Beste\Json\Tests;

use Beste\Json;
use InvalidArgumentException;
use UnexpectedValueException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -28,7 +28,7 @@ public function it_does_not_escape_unicode(): void
/** @test */
public function it_rejects_invalid_resources(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(UnexpectedValueException::class);

// The point is that resources cannot be encoded, not what's in the file :)
Json::encode(fopen(__DIR__.'/valid.json', 'rb'));
Expand Down

0 comments on commit 7102e98

Please sign in to comment.