Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Driver/Cassandra/CassandraModelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Aternos\Model\Driver\Cassandra;

use Aternos\Model\WrappingModelException;
use Aternos\Model\ModelException;
use Throwable;

class CassandraModelException extends WrappingModelException
class CassandraModelException extends ModelException
{
/**
* Wrap an existing exception into a CassandraModelException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

namespace Aternos\Model;
namespace Aternos\Model\Driver\Redis\Exception;

use Throwable;

class WrappingModelException extends ModelException
/**
* Exception that is thrown when a connection to Redis fails
*/
class RedisConnectionException extends RedisModelException
{
/**
* Wrap an existing exception into a ModelException
* Wrap an existing redis exception into a RedisConnectionException
* This is used to adapt exceptions from the driver extensions to a ModelException
* @param Throwable $exception
* @return static
Expand All @@ -16,9 +19,4 @@ static function wrapping(Throwable $exception): static
{
return new static($exception->getMessage(), $exception->getCode(), $exception);
}

final protected function __construct(string $message, int $code = null, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
10 changes: 10 additions & 0 deletions src/Driver/Redis/Exception/RedisModelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Aternos\Model\Driver\Redis\Exception;

use Aternos\Model\ModelException;

abstract class RedisModelException extends ModelException
{

}
24 changes: 24 additions & 0 deletions src/Driver/Redis/Exception/RedisQueryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Aternos\Model\Driver\Redis\Exception;

use Redis;

/**
* Exception that is thrown when a Redis query fails
*/
class RedisQueryException extends RedisModelException
{
/**
* @param Redis $redis
* @return void
* @throws RedisQueryException if the Redis instance has an error
*/
public static function checkConnection(Redis $redis): void
{
$error = $redis->getLastError();
if ($error !== false) {
throw new static("Redis Query Error: " . $error);
}
}
}
20 changes: 14 additions & 6 deletions src/Driver/Redis/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Aternos\Model\Driver\Driver;
use Aternos\Model\Driver\Features\CacheableInterface;
use Aternos\Model\Driver\Features\CRUDAbleInterface;
use Aternos\Model\Driver\Redis\Exception\RedisConnectionException;
use Aternos\Model\Driver\Redis\Exception\RedisModelException;
use Aternos\Model\Driver\Redis\Exception\RedisQueryException;
use Aternos\Model\ModelInterface;
use RedisException;

Expand Down Expand Up @@ -80,7 +83,7 @@ protected function connect(): void
$this->connection->connect($this->socket);
}
} catch (RedisException $e) {
throw RedisModelException::wrapping($e);
throw RedisConnectionException::wrapping($e);
}
}
}
Expand Down Expand Up @@ -113,10 +116,12 @@ public function save(ModelInterface $model): bool
$this->connect();
$key = $this->generateCacheKey($model::class, $model->getId());
try {
return $this->connection->set($key, json_encode($model), $model->getCacheTime());
$this->connection->set($key, json_encode($model), $model->getCacheTime());
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
throw RedisModelException::wrapping($e);
throw RedisConnectionException::wrapping($e);
}
return true;
}

/**
Expand All @@ -137,8 +142,9 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
$this->connect();
try {
$rawData = $this->connection->get($this->generateCacheKey($modelClass, $id));
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
throw RedisModelException::wrapping($e);
throw RedisConnectionException::wrapping($e);
}

if (!$rawData) {
Expand Down Expand Up @@ -172,10 +178,12 @@ public function delete(ModelInterface $model): bool
$this->connect();
$key = $this->generateCacheKey($model::class, $model->getId());
try {
return $this->connection->del($key);
$this->connection->del($key);
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
throw RedisModelException::wrapping($e);
throw RedisConnectionException::wrapping($e);
}
return true;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Driver/Redis/RedisModelException.php

This file was deleted.

Loading