Redefine exceptions

This commit is contained in:
Andrea Marco Sartori 2023-02-25 20:39:46 +10:00
parent 961b8a8278
commit efba19ec9b
11 changed files with 98 additions and 70 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Cerbero\JsonParser\Decoders\DecodedValue;
use Exception;
/**
* The exception thrown when a JSON value cannot be decoded.
*
*/
final class DecodingException extends Exception implements JsonParserException
{
/**
* Instantiate the class
*
* @param DecodedValue $decoded
*/
public function __construct(public DecodedValue $decoded)
{
parent::__construct('Decoding error: ' . $decoded->error, $decoded->code);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The exception thrown when Guzzle is not installed.
*
*/
final class GuzzleRequiredException extends Exception implements JsonParserException
{
/**
* Instantiate the class.
*
*/
public function __construct()
{
parent::__construct('Guzzle is required to load JSON from endpoints');
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The exception thrown when a JSON pointer syntax is not valid.
*
*/
final class InvalidPointerException extends Exception implements JsonParserException
{
/**
* Instantiate the class.
*
* @param string $pointer
*/
public function __construct(public string $pointer)
{
parent::__construct("The string [$pointer] is not a valid JSON pointer");
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The exception thrown when a pointer-related error occurs.
*
*/
final class PointerException extends Exception implements JsonParserException
{
public const CODE_INVALID = 0;
/**
* Retrieve the exception when the given pointer is invalid
*
* @param string $pointer
* @return static
*/
public static function invalid(string $pointer): static
{
return new static("The string [$pointer] is not a valid JSON pointer", static::CODE_INVALID);
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The exception thrown when a source-related error occurs.
*
*/
final class SourceException extends Exception implements JsonParserException
{
public const CODE_UNSUPPORTED = 0;
public const CODE_GUZZLE = 1;
/**
* Retrieve the exception when a JSON source is not supported
*
* @return static
*/
public static function unsupported(): static
{
return new static('Unable to load JSON from the provided source', static::CODE_UNSUPPORTED);
}
/**
* Retrieve the exception when Guzzle is required
*
* @return static
*/
public static function requireGuzzle(): static
{
return new static('Guzzle is required to load JSON from endpoints', static::CODE_GUZZLE);
}
}

View File

@ -5,7 +5,7 @@ namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The syntax exception.
* The exception thrown when the JSON syntax is not valid.
*
*/
final class SyntaxException extends Exception implements JsonParserException
@ -18,6 +18,6 @@ final class SyntaxException extends Exception implements JsonParserException
*/
public function __construct(public string $value, public int $position)
{
parent::__construct("Syntax error: unexpected [$value] at position {$position}");
parent::__construct("Syntax error: unexpected '$value' at position {$position}");
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Cerbero\JsonParser\Exceptions;
use Exception;
/**
* The exception thrown when a JSON source is not supported.
*
*/
final class UnsupportedSourceException extends Exception implements JsonParserException
{
/**
* Instantiate the class.
*
* @param mixed $source
*/
public function __construct(public mixed $source)
{
parent::__construct('Unable to load JSON from the provided source');
}
}

View File

@ -2,7 +2,7 @@
namespace Cerbero\JsonParser\Pointers;
use Cerbero\JsonParser\Exceptions\PointerException;
use Cerbero\JsonParser\Exceptions\InvalidPointerException;
use Cerbero\JsonParser\Tree;
use Closure;
use Stringable;
@ -67,7 +67,7 @@ final class Pointer implements Stringable
private function toReferenceTokens(): array
{
if (preg_match('#^(?:/(?:(?:[^/~])|(?:~[01]))*)*$#', $this->pointer) === 0) {
throw PointerException::invalid($this->pointer);
throw new InvalidPointerException($this->pointer);
}
$tokens = explode('/', $this->pointer);

View File

@ -2,7 +2,7 @@
namespace Cerbero\JsonParser\Sources;
use Cerbero\JsonParser\Exceptions\SourceException;
use Cerbero\JsonParser\Exceptions\UnsupportedSourceException;
use Generator;
use Traversable;
@ -41,7 +41,7 @@ class AnySource extends Source
* Retrieve the JSON fragments
*
* @return Traversable<int, string>
* @throws SourceException
* @throws UnsupportedSourceException
*/
public function getIterator(): Traversable
{
@ -52,7 +52,7 @@ class AnySource extends Source
* Retrieve the matching source
*
* @return Source
* @throws SourceException
* @throws UnsupportedSourceException
*/
protected function matchingSource(): Source
{
@ -66,7 +66,7 @@ class AnySource extends Source
}
}
throw SourceException::unsupported();
throw new UnsupportedSourceException($this->source);
}
/**

View File

@ -31,7 +31,7 @@ class Endpoint extends Source
* Retrieve the JSON fragments
*
* @return Traversable<int, string>
* @throws \Cerbero\JsonParser\Exceptions\SourceException
* @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
*/
public function getIterator(): Traversable
{

View File

@ -27,7 +27,7 @@ class Psr7Request extends Source
* Retrieve the JSON fragments
*
* @return Traversable<int, string>
* @throws \Cerbero\JsonParser\Exceptions\SourceException
* @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
*/
public function getIterator(): Traversable
{