]> git.mxchange.org Git - friendica.git/commitdiff
- Moved the description for the specific storage exception first
authorPhilipp <admin@philipp.info>
Tue, 10 Aug 2021 19:30:40 +0000 (21:30 +0200)
committerPhilipp <admin@philipp.info>
Mon, 16 Aug 2021 21:27:42 +0000 (23:27 +0200)
- Introduced exceptions for try to get invalid storage
- ReferenceStorageException now extends StorageException

doc/AddonStorageBackend.md
src/Core/StorageManager.php
src/Model/Storage/ISelectableStorage.php
src/Model/Storage/ReferenceStorageException.php

index 72e146779326829a77c40b69d11857f944bc9162..b9b7182471871e165e30ee11ff40f6ebc7665fe3 100644 (file)
@@ -122,6 +122,14 @@ abstract class StorageTest
 
 There are two intended types of exceptions for storages
 
+### `ReferenceStorageExecption`
+
+This storage exception should be used in case the caller tries to use an invalid references.
+This could happen in case the caller tries to delete or update an unknown reference.
+The implementation of the storage backend must not ignore invalid references.
+
+Avoid throwing the common `StorageExecption` instead of the `ReferenceStorageException` at this particular situation!
+
 ### `StorageException`
 
 This is the common exception in case unexpected errors happen using the storage backend.
@@ -145,14 +153,6 @@ class ExampleStorage implements ISelectableStorage
 } 
 ```
 
-### `ReferenceStorageExecption`
-
-This storage exception should be used in case the caller tries to use an invalid references.
-This could happen in case the caller tries to delete or update an unknown reference.
-The implementation of the storage backend must not ignore invalid references.
-
-Avoid throwing the common `StorageExecption` instead of the `ReferenceStorageException` at this particular situation!
-
 ## Example
 
 Here an hypotetical addon which register a useless storage backend.
index 03f89f312a6e3d4faffafd683086bfa32f30d2e7..4603b0fdeda50e16df4cf5c55666dd8f0f736d5c 100644 (file)
@@ -25,7 +25,7 @@ use Exception;
 use Friendica\Core\Config\IConfig;
 use Friendica\Database\Database;
 use Friendica\Model\Storage;
-use Friendica\Network\IHTTPRequest;
+use Friendica\Network\HTTPException\InternalServerErrorException;
 use Psr\Log\LoggerInterface;
 
 
@@ -98,11 +98,12 @@ class StorageManager
        /**
         * Returns a selectable storage backend class by registered name
         *
-        * @param string|null $name            Backend name
+        * @param string $name Backend name
         *
-        * @return Storage\ISelectableStorage|null null if no backend registered at $name
+        * @return Storage\ISelectableStorage
         *
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws Storage\ReferenceStorageException in case there's no backend class for the name
+        * @throws Storage\StorageException in case of an unexpected failure during the hook call
         */
        public function getSelectableStorageByName(string $name = null)
        {
@@ -127,16 +128,20 @@ class StorageManager
                                                        'name'    => $name,
                                                        'storage' => null,
                                                ];
-                                               Hook::callAll('storage_instance', $data);
-                                               if (($data['storage'] ?? null) instanceof Storage\ISelectableStorage) {
-                                                       $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
-                                               } else {
-                                                       return null;
+                                               try {
+                                                       Hook::callAll('storage_instance', $data);
+                                                       if (($data['storage'] ?? null) instanceof Storage\ISelectableStorage) {
+                                                               $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
+                                                       } else {
+                                                               throw new Storage\ReferenceStorageException(sprintf('Backend %s was not found', $name));
+                                                       }
+                                               } catch (InternalServerErrorException $exception) {
+                                                       throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
                                                }
                                                break;
                                }
                        } else {
-                               return null;
+                               throw new Storage\ReferenceStorageException(sprintf('Backend %s is not valid', $name));
                        }
                }
 
@@ -146,13 +151,14 @@ class StorageManager
        /**
         * Return storage backend class by registered name
         *
-        * @param string|null $name            Backend name
+        * @param string $name Backend name
         *
-        * @return Storage\IStorage|null null if no backend registered at $name
+        * @return Storage\IStorage
         *
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws Storage\ReferenceStorageException in case there's no backend class for the name
+        * @throws Storage\StorageException in case of an unexpected failure during the hook call
         */
-       public function getByName(string $name = null)
+       public function getByName(string $name)
        {
                // @todo 2020.09 Remove this call after 2 releases
                $name = $this->checkLegacyBackend($name);
@@ -182,16 +188,20 @@ class StorageManager
                                                        'name'    => $name,
                                                        'storage' => null,
                                                ];
-                                               Hook::callAll('storage_instance', $data);
-                                               if (($data['storage'] ?? null) instanceof Storage\IStorage) {
-                                                       $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
-                                               } else {
-                                                       return null;
+                                               try {
+                                                       Hook::callAll('storage_instance', $data);
+                                                       if (($data['storage'] ?? null) instanceof Storage\IStorage) {
+                                                               $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
+                                                       } else {
+                                                               throw new Storage\ReferenceStorageException(sprintf('Backend %s was not found', $name));
+                                                       }
+                                               } catch (InternalServerErrorException $exception) {
+                                                       throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
                                                }
                                                break;
                                }
                        } else {
-                               return null;
+                               throw new Storage\ReferenceStorageException(sprintf('Backend %s is not valid', $name));
                        }
                }
 
index f1477a79cf464d63fb637fe4ca7607eb9b2de029..d3b7547774a8f489deaad33d48be0132866a4fd5 100644 (file)
@@ -23,6 +23,9 @@ namespace Friendica\Model\Storage;
 
 /**
  * Interface for selectable storage backends
+ *
+ * Used for storages with CRUD functionality, mainly used for user data (e.g. photos, attachements).
+ * There's only one active, selectable storage possible and can be selected by the current administrator
  */
 interface ISelectableStorage extends IStorage
 {
index c4de1f879e6c358bc7a05e1fddaef362300cc415..fcfd3ab59d575ac27fc7d8782a5d9ca5729a0ccf 100644 (file)
 
 namespace Friendica\Model\Storage;
 
-use Exception;
-
 /**
  * Storage Exception in case of invalid references
  */
-class ReferenceStorageException extends Exception
+class ReferenceStorageException extends StorageException
 {
 }