DbObject: trigger 404 where we get no UUID

This commit is contained in:
Thomas Gelf 2022-02-06 13:18:58 +01:00
parent 4792859657
commit a05300dc24
2 changed files with 29 additions and 2 deletions

View File

@ -1239,7 +1239,7 @@ abstract class DbObject
if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) {
$table = $obj->getTableName();
assert($connection instanceof Db);
$uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch());
$uuid = UuidLookup::requireUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch());
return self::$dbObjectStore->load($table, $uuid);
}
@ -1268,7 +1268,7 @@ abstract class DbObject
if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) {
$table = $obj->getTableName();
assert($connection instanceof Db);
$uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch());
$uuid = UuidLookup::requireUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch());
return self::$dbObjectStore->load($table, $uuid);
}

View File

@ -2,10 +2,12 @@
namespace Icinga\Module\Director\Db\Branch;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use function is_int;
use function is_resource;
@ -57,6 +59,31 @@ class UuidLookup
return $uuid;
}
/**
* @param int|string|array $key
* @param string $table
* @param Db $connection
* @param Branch $branch
* @return UuidInterface
* @throws NotFoundError
*/
public static function requireUuidForKey($key, $table, Db $connection, Branch $branch)
{
$uuid = self::findUuidForKey($key, $table, $connection, $branch);
if ($uuid === null) {
throw new NotFoundError('No such object available');
}
return $uuid;
}
/**
* @param int|string|array $key
* @param string $table
* @param Db $connection
* @param Branch $branch
* @return ?UuidInterface
*/
public static function findUuidForKey($key, $table, Db $connection, Branch $branch)
{
$db = $connection->getDbAdapter();