diff --git a/CloudObjects/SDK/Exceptions/CoreAPIException.php b/CloudObjects/SDK/Exceptions/CoreAPIException.php index 50d0859..94fdbf2 100644 --- a/CloudObjects/SDK/Exceptions/CoreAPIException.php +++ b/CloudObjects/SDK/Exceptions/CoreAPIException.php @@ -11,4 +11,12 @@ namespace CloudObjects\SDK\Exceptions; */ class CoreAPIException extends SDKException { + /** + * @param string $message The error message. + * @param \Throwable|null $previous The original exception that caused this error, if any. + */ + public function __construct(string $message = "", ?\Throwable $previous = null) { + parent::__construct($message, 0, $previous); + } + } diff --git a/CloudObjects/SDK/ObjectRetriever.php b/CloudObjects/SDK/ObjectRetriever.php index f39f385..fb622b0 100644 --- a/CloudObjects/SDK/ObjectRetriever.php +++ b/CloudObjects/SDK/ObjectRetriever.php @@ -283,11 +283,24 @@ class ObjectRetriever implements CustomCacheAndLogInterface { $this->putIntoCache($uriString, $object, $this->options['cache_ttl']); $this->logInfoWithTime('Fetched <'.$uriString.'> from Core API ['.$response->getStatusCode().'].', $ts); } catch (RequestException $e) { - if ($e->hasResponse()) - $this->logInfoWithTime('Object <'.$uriString.'> not found in Core API ['.$e->getResponse()->getStatusCode().'].', $ts); - else - $this->logInfoWithTime('Object <'.$uriString.'> could not be retrieved from Core API.', $ts); - return null; + if ($e->hasResponse() && $e->getResponse()->getStatusCode() == 404) { + // not found + $this->logInfoWithTime('Object <'.$uriString.'> not found in Core API.', $ts); + return null; + } else + if ($e->hasResponse()) { + // other API error + $this->logInfoWithTime('Object <'.$uriString.'> could not be retrieved from Core API ['.$e->getResponse()->getStatusCode().'].', $ts); + throw new CoreAPIException($e->getMessage(), $e); + } else { + // API error without response + $this->logInfoWithTime('Object <'.$uriString.'> could not be retrieved from Core API (no status code).', $ts); + throw new CoreAPIException($e->getMessage(), $e); + } + } catch (Exception $e) { + // other exceptions + $this->logInfoWithTime('Object <'.$uriString.'> could not be retrieved from Core API (unexpected exception).', $ts); + throw new CoreAPIException($e->getMessage(), $e); } } @@ -337,7 +350,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { $this->logInfoWithTime('Fetched all objects with <'.$type.'> for <'.$namespaceCoid->getHost().'> from Core API ['.$response->getStatusCode().'].', $ts); } catch (Exception $e) { - throw new CoreAPIException; + throw new CoreAPIException($e->getMessage(), $e); } return $allIris; @@ -379,7 +392,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { $this->logInfoWithTime('Fetched all objects for <'.$namespaceCoid->getHost().'> from Core API ['.$response->getStatusCode().'].', $ts); } catch (Exception $e) { - throw new CoreAPIException; + throw new CoreAPIException($e->getMessage(), $e); } return $allIris; @@ -413,7 +426,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { $this->logInfoWithTime('Fetched object list for <'.$namespaceCoid->getHost().'> from Core API ['.$response->getStatusCode().'].', $ts); } catch (Exception $e) { - throw new CoreAPIException; + throw new CoreAPIException($e->getMessage(), $e); } return $allIris; @@ -452,7 +465,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface { $this->logInfoWithTime('Fetched object list with <'.$type.'> for <'.$namespaceCoid->getHost().'> from Core API ['.$response->getStatusCode().'].', $ts); } catch (Exception $e) { - throw new CoreAPIException; + throw new CoreAPIException($e->getMessage(), $e); } return $allIris; diff --git a/tests/OfflineTests/ObjectRetrieverMockTest.php b/tests/OfflineTests/ObjectRetrieverMockTest.php index 034a304..6e88d70 100644 --- a/tests/OfflineTests/ObjectRetrieverMockTest.php +++ b/tests/OfflineTests/ObjectRetrieverMockTest.php @@ -9,6 +9,7 @@ namespace CloudObjects\SDK; use ML\IRI\IRI; use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler, GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response; +use CloudObjects\SDK\Exceptions\CoreAPIException; class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase { @@ -50,4 +51,23 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase { $this->assertNull($object->getString('urn:example:nonexistingvalue')); } + public function testGetNonExistingResource() { + $this->setMockResponse(new Response(404)); + + // should return "null" + $coid = new IRI('coid://nonexistingresource.example.com'); + + $object = $this->retriever->getObjectNode($coid); + $this->assertNull($object); + } + + public function testWhenAPIThrowsError() { + $this->setMockResponse(new Response(500)); + + $coid = new IRI('coid://nonexistingresource.example.com'); + + $this->expectException(CoreAPIException::class); + $this->retriever->getObjectNode($coid); + } + }