Introduced common exception superclass
This commit is contained in:
1 parent
f749d4a05e
commit
afafb1ac3d
12 files changed
+48
-28
No files matched your search
@@ -13,6 +13,7 @@ use Nyholm\Psr7\Factory\Psr17Factory;
|
|||||||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
|
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
|
||||||
use GuzzleHttp\Client, GuzzleHttp\HandlerStack, GuzzleHttp\Middleware;
|
use GuzzleHttp\Client, GuzzleHttp\HandlerStack, GuzzleHttp\Middleware;
|
||||||
use Psr\Http\Message\RequestInterface, Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\RequestInterface, Psr\Http\Message\ResponseInterface;
|
||||||
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The context of an request for an account.
|
* The context of an request for an account.
|
||||||
@@ -42,7 +43,7 @@ class AccountContext {
|
|||||||
*/
|
*/
|
||||||
public function __construct(IRI $aauid, $accessToken, ?DataLoader $dataLoader = null) {
|
public function __construct(IRI $aauid, $accessToken, ?DataLoader $dataLoader = null) {
|
||||||
if (AAUIDParser::getType($aauid) != AAUIDParser::AAUID_ACCOUNT)
|
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->aauid = $aauid;
|
||||||
$this->accessToken = $accessToken;
|
$this->accessToken = $accessToken;
|
||||||
@@ -324,7 +325,7 @@ class AccountContext {
|
|||||||
*/
|
*/
|
||||||
public function setLogCode($logCode) {
|
public function setLogCode($logCode) {
|
||||||
if (!$this->request) {
|
if (!$this->request) {
|
||||||
throw new \Exception('Not in a request context.');
|
throw new SDKException('Not in a request context.');
|
||||||
}
|
}
|
||||||
$this->logCode = $logCode;
|
$this->logCode = $logCode;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace CloudObjects\SDK\Common;
|
namespace CloudObjects\SDK\Common;
|
||||||
|
|
||||||
use Exception;
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
use ML\IRI\IRI;
|
use ML\IRI\IRI;
|
||||||
use CloudObjects\SDK\NodeReader;
|
use CloudObjects\SDK\NodeReader;
|
||||||
use Defuse\Crypto\Key, Defuse\Crypto\Crypto;
|
use Defuse\Crypto\Key, Defuse\Crypto\Crypto;
|
||||||
@@ -55,7 +55,7 @@ class CryptoHelper {
|
|||||||
*/
|
*/
|
||||||
public function __construct(ObjectRetriever $objectRetriever, ?IRI $namespaceCoid = null) {
|
public function __construct(ObjectRetriever $objectRetriever, ?IRI $namespaceCoid = null) {
|
||||||
if (!class_exists('Defuse\Crypto\Crypto'))
|
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->objectRetriever = $objectRetriever;
|
||||||
$this->namespace = isset($namespaceCoid)
|
$this->namespace = isset($namespaceCoid)
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ namespace CloudObjects\SDK\Exceptions;
|
|||||||
/**
|
/**
|
||||||
* An Exception that is thrown when the Core API returned an error.
|
* An Exception that is thrown when the Core API returned an error.
|
||||||
*/
|
*/
|
||||||
class CoreAPIException extends \Exception {
|
class CoreAPIException extends SDKException {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,6 @@ namespace CloudObjects\SDK\Exceptions;
|
|||||||
* An Exception that is thrown when an object's configuration
|
* An Exception that is thrown when an object's configuration
|
||||||
* doesn't match the client's expectations.
|
* doesn't match the client's expectations.
|
||||||
*/
|
*/
|
||||||
class InvalidObjectConfigurationException extends \Exception {
|
class InvalidObjectConfigurationException extends SDKException {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,6 @@ namespace CloudObjects\SDK\Exceptions;
|
|||||||
/**
|
/**
|
||||||
* An Exception that is thrown when the SDK's configuration isn't valid.
|
* An Exception that is thrown when the SDK's configuration isn't valid.
|
||||||
*/
|
*/
|
||||||
class InvalidSDKConfigurationException extends \Exception {
|
class InvalidSDKConfigurationException extends SDKException {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
namespace CloudObjects\SDK\Exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The common superclass for all Exceptions thrown by the SDK.
|
||||||
|
* It is also thrown directly for errors that have no more specific Exception.
|
||||||
|
*/
|
||||||
|
class SDKException extends \Exception {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace CloudObjects\SDK\Helpers;
|
namespace CloudObjects\SDK\Helpers;
|
||||||
|
|
||||||
use Exception;
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
use CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
|
use CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
|
||||||
use CloudObjects\SDK\NodeReader, CloudObjects\SDK\ObjectRetriever;
|
use CloudObjects\SDK\NodeReader, CloudObjects\SDK\ObjectRetriever;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ class SDKLoader {
|
|||||||
* @param string $sdkName The name of the SDK to initialize.
|
* @param string $sdkName The name of the SDK to initialize.
|
||||||
* @param array $options Additional options for the SDK (if necessary).
|
* @param array $options Additional options for the SDK (if necessary).
|
||||||
* @return mixed The return value of the SDK initialization, which may vary depending on the SDK.
|
* @return mixed The return value of the SDK initialization, which may vary depending on the SDK.
|
||||||
* @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 init(string $sdkName, array $options = []) : mixed {
|
public function init(string $sdkName, array $options = []) : mixed {
|
||||||
$namespace = $this->objectRetriever->getAuthenticatingNamespaceCloudObject();
|
$namespace = $this->objectRetriever->getAuthenticatingNamespaceCloudObject();
|
||||||
@@ -52,22 +52,22 @@ class SDKLoader {
|
|||||||
], $options));
|
], $options));
|
||||||
|
|
||||||
default:
|
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.
|
* 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 string $classname Classname for the SDK's main class.
|
||||||
* @param array $options Additional options for the SDK (if necessary).
|
* @param array $options Additional options for the SDK (if necessary).
|
||||||
* @return mixed The initialized SDK instance.
|
* @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 {
|
public function get(string $classname, array $options) : mixed {
|
||||||
if (!class_exists($classname))
|
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));
|
$hashkey = md5($classname.serialize($options));
|
||||||
if (!isset($this->classes[$hashkey])) {
|
if (!isset($this->classes[$hashkey])) {
|
||||||
@@ -110,7 +110,7 @@ class SDKLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($class))
|
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;
|
$this->classes[$hashkey] = $class;
|
||||||
return $this->classes[$hashkey];
|
return $this->classes[$hashkey];
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ use Kevinrob\GuzzleCache\Storage\CacheStorageInterface,
|
|||||||
use Psr\Cache\CacheItemPoolInterface;
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use CloudObjects\SDK\Exceptions\CoreAPIException,
|
use CloudObjects\SDK\Exceptions\CoreAPIException,
|
||||||
CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
|
CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException,
|
||||||
|
CloudObjects\SDK\Exceptions\SDKException;
|
||||||
use CloudObjects\SDK\AccountGateway\AccountContext;
|
use CloudObjects\SDK\AccountGateway\AccountContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -241,7 +242,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
*/
|
*/
|
||||||
public function getObjectNode(IRI $coid) : ?Node {
|
public function getObjectNode(IRI $coid) : ?Node {
|
||||||
if (!COIDParser::isValidCOID($coid))
|
if (!COIDParser::isValidCOID($coid))
|
||||||
throw new Exception("Not a valid COID.");
|
throw new SDKException("Not a valid COID.");
|
||||||
|
|
||||||
$uriString = (string)$coid;
|
$uriString = (string)$coid;
|
||||||
|
|
||||||
@@ -308,7 +309,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
*/
|
*/
|
||||||
public function fetchObjectsInNamespaceWithType(IRI $namespaceCoid, $type) {
|
public function fetchObjectsInNamespaceWithType(IRI $namespaceCoid, $type) {
|
||||||
if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT)
|
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);
|
$ts = microtime(true);
|
||||||
$type = (string)$type;
|
$type = (string)$type;
|
||||||
@@ -353,7 +354,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
*/
|
*/
|
||||||
public function fetchAllObjectsInNamespace(IRI $namespaceCoid) {
|
public function fetchAllObjectsInNamespace(IRI $namespaceCoid) {
|
||||||
if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT)
|
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);
|
$ts = microtime(true);
|
||||||
|
|
||||||
@@ -394,7 +395,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
*/
|
*/
|
||||||
public function getCOIDListForNamespace(IRI $namespaceCoid) {
|
public function getCOIDListForNamespace(IRI $namespaceCoid) {
|
||||||
if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT)
|
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);
|
$ts = microtime(true);
|
||||||
|
|
||||||
@@ -429,7 +430,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
*/
|
*/
|
||||||
public function getCOIDListForNamespaceWithType(IRI $namespaceCoid, $type) {
|
public function getCOIDListForNamespaceWithType(IRI $namespaceCoid, $type) {
|
||||||
if (COIDParser::getType($namespaceCoid) != COIDParser::COID_ROOT)
|
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);
|
$ts = microtime(true);
|
||||||
$type = (string)$type;
|
$type = (string)$type;
|
||||||
@@ -472,7 +473,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
|||||||
if (is_object($coid) && get_class($coid)=='ML\IRI\IRI')
|
if (is_object($coid) && get_class($coid)=='ML\IRI\IRI')
|
||||||
return $this->getObject($coid);
|
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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace CloudObjects\SDK;
|
namespace CloudObjects\SDK;
|
||||||
|
|
||||||
use Exception;
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
use ML\IRI\IRI;
|
use ML\IRI\IRI;
|
||||||
use ML\JsonLD\Node;
|
use ML\JsonLD\Node;
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ class ObjectRetrieverFacade {
|
|||||||
|
|
||||||
private static function getInstance() : ObjectRetriever {
|
private static function getInstance() : ObjectRetriever {
|
||||||
if (self::$instance === null)
|
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;
|
return self::$instance;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ use GuzzleHttp\Client, GuzzleHttp\HandlerStack, GuzzleHttp\Middleware;
|
|||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use CloudObjects\SDK\COIDParser, CloudObjects\SDK\ObjectRetriever;
|
use CloudObjects\SDK\COIDParser, CloudObjects\SDK\ObjectRetriever;
|
||||||
use CloudObjects\SDK\Exceptions\InvalidObjectConfigurationException,
|
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
|
* 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 ($this->reader->hasType($api, 'wa:GraphQLEndpoint')) {
|
||||||
if (!class_exists('GraphQL\Client'))
|
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'],
|
return new \GraphQL\Client($clientConfig['base_uri'],
|
||||||
isset($clientConfig['headers']) ? $clientConfig['headers'] : []);
|
isset($clientConfig['headers']) ? $clientConfig['headers'] : []);
|
||||||
|
|||||||
@@ -6,9 +6,11 @@
|
|||||||
|
|
||||||
namespace CloudObjects\SDK\WebAPI\Exceptions;
|
namespace CloudObjects\SDK\WebAPI\Exceptions;
|
||||||
|
|
||||||
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An Exception that is thrown when an an OAuth flow failed.
|
* An Exception that is thrown when an an OAuth flow failed.
|
||||||
*/
|
*/
|
||||||
class OAuthFlowException extends \Exception {
|
class OAuthFlowException extends SDKException {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace CloudObjects\SDK\WebAPI;
|
namespace CloudObjects\SDK\WebAPI;
|
||||||
|
|
||||||
use Exception;
|
use CloudObjects\SDK\Exceptions\SDKException;
|
||||||
use ML\JsonLD\Node;
|
use ML\JsonLD\Node;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Webmozart\Assert\Assert,
|
use Webmozart\Assert\Assert,
|
||||||
@@ -114,7 +114,7 @@ class OAuth2AuthServer {
|
|||||||
// no additional params needed
|
// no additional params needed
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Exception("No flow/grant_type found.");
|
throw new SDKException("No flow/grant_type found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$grantCacheKey = sha1(json_encode($params));
|
$grantCacheKey = sha1(json_encode($params));
|
||||||
|
|||||||
Reference in new issue
Block a user