Improved exception handling

This commit is contained in:
lukas committed 2026-10-16 08:38:08 +00:00
1 parent 70c52b8ad2
commit 12ed032a31
3 files changed
+49 -8

No files matched your search

@@ -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);
}
}
+21 -8
View File
@@ -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);
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;
@@ -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);
}
}