From afafb1ac3d735d649f1ef64eb6e9164d94e5a032 Mon Sep 17 00:00:00 2001 From: Lukas Rosenstock Date: Thu, 15 Oct 2026 13:39:44 +0000 Subject: [PATCH] Introduced common exception superclass --- .../SDK/AccountGateway/AccountContext.php | 5 +++-- CloudObjects/SDK/Common/CryptoHelper.php | 4 ++-- CloudObjects/SDK/Exceptions/CoreAPIException.php | 2 +- .../InvalidObjectConfigurationException.php | 2 +- .../InvalidSDKConfigurationException.php | 2 +- CloudObjects/SDK/Exceptions/SDKException.php | 15 +++++++++++++++ CloudObjects/SDK/Helpers/SDKLoader.php | 14 +++++++------- CloudObjects/SDK/ObjectRetriever.php | 15 ++++++++------- CloudObjects/SDK/ObjectRetrieverFacade.php | 4 ++-- CloudObjects/SDK/WebAPI/APIClientFactory.php | 5 +++-- .../SDK/WebAPI/Exceptions/OAuthFlowException.php | 4 +++- CloudObjects/SDK/WebAPI/OAuth2AuthServer.php | 4 ++-- 12 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 CloudObjects/SDK/Exceptions/SDKException.php diff --git a/CloudObjects/SDK/AccountGateway/AccountContext.php b/CloudObjects/SDK/AccountGateway/AccountContext.php index 10cc5fd..0750c2d 100644 --- a/CloudObjects/SDK/AccountGateway/AccountContext.php +++ b/CloudObjects/SDK/AccountGateway/AccountContext.php @@ -13,6 +13,7 @@ use Nyholm\Psr7\Factory\Psr17Factory; use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; use GuzzleHttp\Client, GuzzleHttp\HandlerStack, GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface, Psr\Http\Message\ResponseInterface; +use CloudObjects\SDK\Exceptions\SDKException; /** * The context of an request for an account. @@ -42,7 +43,7 @@ class AccountContext { */ public function __construct(IRI $aauid, $accessToken, ?DataLoader $dataLoader = null) { if (AAUIDParser::getType($aauid) != AAUIDParser::AAUID_ACCOUNT) - throw new \Exception("Not a valid AAUID"); + throw new SDKException("Not a valid AAUID"); $this->aauid = $aauid; $this->accessToken = $accessToken; @@ -324,7 +325,7 @@ class AccountContext { */ public function setLogCode($logCode) { if (!$this->request) { - throw new \Exception('Not in a request context.'); + throw new SDKException('Not in a request context.'); } $this->logCode = $logCode; } diff --git a/CloudObjects/SDK/Common/CryptoHelper.php b/CloudObjects/SDK/Common/CryptoHelper.php index 49af6e6..32819e6 100644 --- a/CloudObjects/SDK/Common/CryptoHelper.php +++ b/CloudObjects/SDK/Common/CryptoHelper.php @@ -6,7 +6,7 @@ namespace CloudObjects\SDK\Common; -use Exception; +use CloudObjects\SDK\Exceptions\SDKException; use ML\IRI\IRI; use CloudObjects\SDK\NodeReader; use Defuse\Crypto\Key, Defuse\Crypto\Crypto; @@ -55,7 +55,7 @@ class CryptoHelper { */ public function __construct(ObjectRetriever $objectRetriever, ?IRI $namespaceCoid = null) { if (!class_exists('Defuse\Crypto\Crypto')) - throw new Exception("Run composer require defuse/php-encryption before using CryptoHelper."); + throw new SDKException("Run composer require defuse/php-encryption before using CryptoHelper."); $this->objectRetriever = $objectRetriever; $this->namespace = isset($namespaceCoid) diff --git a/CloudObjects/SDK/Exceptions/CoreAPIException.php b/CloudObjects/SDK/Exceptions/CoreAPIException.php index 508a3a6..50d0859 100644 --- a/CloudObjects/SDK/Exceptions/CoreAPIException.php +++ b/CloudObjects/SDK/Exceptions/CoreAPIException.php @@ -9,6 +9,6 @@ namespace CloudObjects\SDK\Exceptions; /** * An Exception that is thrown when the Core API returned an error. */ -class CoreAPIException extends \Exception { +class CoreAPIException extends SDKException { } diff --git a/CloudObjects/SDK/Exceptions/InvalidObjectConfigurationException.php b/CloudObjects/SDK/Exceptions/InvalidObjectConfigurationException.php index 85a6bf2..ccfe9f8 100644 --- a/CloudObjects/SDK/Exceptions/InvalidObjectConfigurationException.php +++ b/CloudObjects/SDK/Exceptions/InvalidObjectConfigurationException.php @@ -10,6 +10,6 @@ namespace CloudObjects\SDK\Exceptions; * An Exception that is thrown when an object's configuration * doesn't match the client's expectations. */ -class InvalidObjectConfigurationException extends \Exception { +class InvalidObjectConfigurationException extends SDKException { } diff --git a/CloudObjects/SDK/Exceptions/InvalidSDKConfigurationException.php b/CloudObjects/SDK/Exceptions/InvalidSDKConfigurationException.php index 50745e0..bd603ee 100644 --- a/CloudObjects/SDK/Exceptions/InvalidSDKConfigurationException.php +++ b/CloudObjects/SDK/Exceptions/InvalidSDKConfigurationException.php @@ -9,6 +9,6 @@ namespace CloudObjects\SDK\Exceptions; /** * An Exception that is thrown when the SDK's configuration isn't valid. */ -class InvalidSDKConfigurationException extends \Exception { +class InvalidSDKConfigurationException extends SDKException { } diff --git a/CloudObjects/SDK/Exceptions/SDKException.php b/CloudObjects/SDK/Exceptions/SDKException.php new file mode 100644 index 0000000..3376ce1 --- /dev/null +++ b/CloudObjects/SDK/Exceptions/SDKException.php @@ -0,0 +1,15 @@ +objectRetriever->getAuthenticatingNamespaceCloudObject(); @@ -52,22 +52,22 @@ class SDKLoader { ], $options)); default: - throw new Exception("No rules defined to initialize SDK with name <".$sdkName.">."); + throw new SDKException("No rules defined to initialize SDK with name <".$sdkName.">."); } } /** * Initialize and return the SDK with the given classname. - * Throws Exception if the SDK is not supported. + * Throws SDKException if the SDK is not supported. * * @param string $classname Classname for the SDK's main class. * @param array $options Additional options for the SDK (if necessary). * @return mixed The initialized SDK instance. - * @throws Exception If the SDK is not supported or cannot be initialized. + * @throws SDKException If the SDK is not supported or cannot be initialized. */ public function get(string $classname, array $options) : mixed { if (!class_exists($classname)) - throw new Exception("<".$classname."> is not a valid classname."); + throw new SDKException("<".$classname."> is not a valid classname."); $hashkey = md5($classname.serialize($options)); if (!isset($this->classes[$hashkey])) { @@ -110,7 +110,7 @@ class SDKLoader { } if (!isset($class)) - throw new Exception("No rules defined to initialize <".$classname.">."); + throw new SDKException("No rules defined to initialize <".$classname.">."); $this->classes[$hashkey] = $class; return $this->classes[$hashkey]; diff --git a/CloudObjects/SDK/ObjectRetriever.php b/CloudObjects/SDK/ObjectRetriever.php index 1ca7ea8..f39f385 100644 --- a/CloudObjects/SDK/ObjectRetriever.php +++ b/CloudObjects/SDK/ObjectRetriever.php @@ -23,7 +23,8 @@ use Kevinrob\GuzzleCache\Storage\CacheStorageInterface, use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; use CloudObjects\SDK\Exceptions\CoreAPIException, - CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException; + CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException, + CloudObjects\SDK\Exceptions\SDKException; use CloudObjects\SDK\AccountGateway\AccountContext; /** @@ -241,7 +242,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { */ public function getObjectNode(IRI $coid) : ?Node { if (!COIDParser::isValidCOID($coid)) - throw new Exception("Not a valid COID."); + throw new SDKException("Not a valid COID."); $uriString = (string)$coid; @@ -308,7 +309,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { */ public function fetchObjectsInNamespaceWithType(IRI $namespaceCoid, $type) { if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT) - throw new Exception("Not a valid namespace COID."); + throw new SDKException("Not a valid namespace COID."); $ts = microtime(true); $type = (string)$type; @@ -353,7 +354,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { */ public function fetchAllObjectsInNamespace(IRI $namespaceCoid) { if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT) - throw new Exception("Not a valid namespace COID."); + throw new SDKException("Not a valid namespace COID."); $ts = microtime(true); @@ -394,7 +395,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { */ public function getCOIDListForNamespace(IRI $namespaceCoid) { if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT) - throw new Exception("Not a valid namespace COID."); + throw new SDKException("Not a valid namespace COID."); $ts = microtime(true); @@ -429,7 +430,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { */ public function getCOIDListForNamespaceWithType(IRI $namespaceCoid, $type) { if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT) - throw new Exception("Not a valid namespace COID."); + throw new SDKException("Not a valid namespace COID."); $ts = microtime(true); $type = (string)$type; @@ -472,7 +473,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { if (is_object($coid) && get_class($coid)=='ML\IRI\IRI') return $this->getObject($coid); - throw new Exception('COID must be passed as a string or an IRI object.'); + throw new SDKException('COID must be passed as a string or an IRI object.'); } /** diff --git a/CloudObjects/SDK/ObjectRetrieverFacade.php b/CloudObjects/SDK/ObjectRetrieverFacade.php index d804142..fc3fdfc 100644 --- a/CloudObjects/SDK/ObjectRetrieverFacade.php +++ b/CloudObjects/SDK/ObjectRetrieverFacade.php @@ -6,7 +6,7 @@ namespace CloudObjects\SDK; -use Exception; +use CloudObjects\SDK\Exceptions\SDKException; use ML\IRI\IRI; use ML\JsonLD\Node; @@ -24,7 +24,7 @@ class ObjectRetrieverFacade { private static function getInstance() : ObjectRetriever { if (self::$instance === null) - throw new Exception('ObjectRetrieverFacade has not been initialized. Call setObjectRetriever() first.'); + throw new SDKException('ObjectRetrieverFacade has not been initialized. Call setObjectRetriever() first.'); return self::$instance; } diff --git a/CloudObjects/SDK/WebAPI/APIClientFactory.php b/CloudObjects/SDK/WebAPI/APIClientFactory.php index 67e09f4..acbfa2b 100644 --- a/CloudObjects/SDK/WebAPI/APIClientFactory.php +++ b/CloudObjects/SDK/WebAPI/APIClientFactory.php @@ -14,7 +14,8 @@ use GuzzleHttp\Client, GuzzleHttp\HandlerStack, GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; use CloudObjects\SDK\COIDParser, CloudObjects\SDK\ObjectRetriever; use CloudObjects\SDK\Exceptions\InvalidObjectConfigurationException, - CloudObjects\SDK\Exceptions\CoreAPIException; + CloudObjects\SDK\Exceptions\CoreAPIException, + CloudObjects\SDK\Exceptions\SDKException; /** * The APIClientFactory can be used to create a preconfigured Guzzle HTTP API client @@ -205,7 +206,7 @@ class APIClientFactory { if ($this->reader->hasType($api, 'wa:GraphQLEndpoint')) { if (!class_exists('GraphQL\Client')) - throw new Exception("Install the gmostafa/php-graphql-client package to retrieve a specific client for wa:GraphQLEndpoint objects."); + throw new SDKException("Install the gmostafa/php-graphql-client package to retrieve a specific client for wa:GraphQLEndpoint objects."); return new \GraphQL\Client($clientConfig['base_uri'], isset($clientConfig['headers']) ? $clientConfig['headers'] : []); diff --git a/CloudObjects/SDK/WebAPI/Exceptions/OAuthFlowException.php b/CloudObjects/SDK/WebAPI/Exceptions/OAuthFlowException.php index 0f7c0f6..7332c60 100644 --- a/CloudObjects/SDK/WebAPI/Exceptions/OAuthFlowException.php +++ b/CloudObjects/SDK/WebAPI/Exceptions/OAuthFlowException.php @@ -6,9 +6,11 @@ namespace CloudObjects\SDK\WebAPI\Exceptions; +use CloudObjects\SDK\Exceptions\SDKException; + /** * An Exception that is thrown when an an OAuth flow failed. */ -class OAuthFlowException extends \Exception { +class OAuthFlowException extends SDKException { } diff --git a/CloudObjects/SDK/WebAPI/OAuth2AuthServer.php b/CloudObjects/SDK/WebAPI/OAuth2AuthServer.php index fb656d0..75bcec1 100644 --- a/CloudObjects/SDK/WebAPI/OAuth2AuthServer.php +++ b/CloudObjects/SDK/WebAPI/OAuth2AuthServer.php @@ -6,7 +6,7 @@ namespace CloudObjects\SDK\WebAPI; -use Exception; +use CloudObjects\SDK\Exceptions\SDKException; use ML\JsonLD\Node; use GuzzleHttp\Client; use Webmozart\Assert\Assert, @@ -114,7 +114,7 @@ class OAuth2AuthServer { // no additional params needed break; default: - throw new Exception("No flow/grant_type found."); + throw new SDKException("No flow/grant_type found."); } $grantCacheKey = sha1(json_encode($params));