Introduce exception codes

This commit is contained in:
Andrea Marco Sartori 2022-11-21 00:35:13 +10:00
parent 76aa756453
commit dea8a49af5
3 changed files with 10 additions and 14 deletions

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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;