mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-29 10:38:09 +01:00
Introduce exception codes
This commit is contained in:
parent
76aa756453
commit
dea8a49af5
@ -10,13 +10,9 @@ use Exception;
|
||||
*/
|
||||
abstract class JsonParserException extends Exception
|
||||
{
|
||||
/**
|
||||
* Enforce factory methods to instantiate exceptions
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
protected function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
public const CODE_SOURCE_INVALID = 0;
|
||||
public const CODE_SOURCE_UNSUPPORTED = 1;
|
||||
public const CODE_SOURCE_GUZZLE = 2;
|
||||
|
||||
public const CODE_POINTER_INVALID = 3;
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ class SourceException extends JsonParserException
|
||||
* @param string $source
|
||||
* @return static
|
||||
*/
|
||||
public static function invalidSource(string $source): static
|
||||
public static function invalid(string $source): static
|
||||
{
|
||||
return new static("[$source] is not a valid source");
|
||||
return new static("[$source] is not a valid source", static::CODE_SOURCE_INVALID);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -26,7 +26,7 @@ class SourceException extends JsonParserException
|
||||
*/
|
||||
public static function unsupported(): static
|
||||
{
|
||||
return new static('Unable to load JSON from the provided source');
|
||||
return new static('Unable to load JSON from the provided source', static::CODE_SOURCE_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,6 +36,6 @@ class SourceException extends JsonParserException
|
||||
*/
|
||||
public static function requireGuzzle(): static
|
||||
{
|
||||
return new static('Guzzle is required to load JSON from endpoints');
|
||||
return new static('Guzzle is required to load JSON from endpoints', static::CODE_SOURCE_GUZZLE);
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ abstract class Source implements IteratorAggregate
|
||||
{
|
||||
foreach ($customSource as $class) {
|
||||
if (!is_subclass_of($class, Source::class)) {
|
||||
throw SourceException::invalidSource($class);
|
||||
throw SourceException::invalid($class);
|
||||
}
|
||||
|
||||
static::$customSources[] = $class;
|
||||
|
Loading…
x
Reference in New Issue
Block a user