mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-29 10:38:09 +01:00
Redefine exceptions
This commit is contained in:
parent
961b8a8278
commit
efba19ec9b
23
src/Exceptions/DecodingException.php
Normal file
23
src/Exceptions/DecodingException.php
Normal 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);
|
||||
}
|
||||
}
|
21
src/Exceptions/GuzzleRequiredException.php
Normal file
21
src/Exceptions/GuzzleRequiredException.php
Normal 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');
|
||||
}
|
||||
}
|
22
src/Exceptions/InvalidPointerException.php
Normal file
22
src/Exceptions/InvalidPointerException.php
Normal 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");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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}");
|
||||
}
|
||||
}
|
||||
|
22
src/Exceptions/UnsupportedSourceException.php
Normal file
22
src/Exceptions/UnsupportedSourceException.php
Normal 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');
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user