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.
}
```
-### `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.
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;
/**
* 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)
{
'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));
}
}
/**
* 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);
'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));
}
}