Error handling simplified

This commit is contained in:
Mal 2020-08-21 22:46:19 +02:00
parent 3cb02a12f1
commit 3a99e99130
2 changed files with 37 additions and 23 deletions

View File

@ -19,7 +19,10 @@ final class MySqlDatabase implements DatabaseInterface
try {
$this->connection = new PDO("mysql:host=$hostname;dbname=$database", $user, $password);
} catch (Throwable $e) {
throw new DatabaseException('Couldn\'t connect to the database!');
throw new DatabaseException(
'Couldn\'t connect to the database!',
ServerStatus::INTERNAL_ERROR
);
}
}
@ -138,7 +141,10 @@ final class MySqlDatabase implements DatabaseInterface
try {
$this->Query($query, $conditions);
} catch (Throwable $e) {
return [];
throw new DatabaseException(
$e->getMessage(),
ServerStatus::INTERNAL_ERROR
);
}
return $this->getResult();

View File

@ -67,7 +67,10 @@ abstract class Table
$type = self::TYPE_BOOL;
break;
default:
throw new Exception(sprintf('Type %s of field %s couldn\'t be handled', $sqlType, $field['Field']));
throw new DatabaseException(
sprintf('Type %s of field %s couldn\'t be handled', $sqlType, $field['Field']),
ServerStatus::INTERNAL_ERROR
);
}
$this->addField($field['Field'], $type);
@ -94,8 +97,9 @@ abstract class Table
protected function addField(string $name, int $type): void
{
if (!self::IsValidType($type)) {
throw new Exception(
sprintf('Field %s has invalid type of %s!', $name, $type)
throw new DatabaseException(
sprintf('Field %s has invalid type of %s!', $name, $type),
ServerStatus::INTERNAL_ERROR
);
}
@ -104,19 +108,18 @@ abstract class Table
protected function loadById($id): void
{
try {
$this->database->Query(
sprintf('SELECT * FROM %s WHERE %s = :id', $this->tableName, $this->primaryKey),
['id' => $id]
);
} catch (Throwable $e) {
throw new Exception();
}
$result = $this->database->getResult();
if (count($result) === 0) {
throw new Exception(sprintf('No %s with id %d found!', $this->tableName, $id));
throw new DatabaseException(
sprintf('No %s with id %d found!', $this->tableName, $id),
ServerStatus::BAD_REQUEST
);
}
foreach ($result[0] as $field => $value) {
@ -131,11 +134,7 @@ abstract class Table
public function Delete(): void
{
try {
$this->database->Delete($this->tableName, [$this->primaryKey => $this->getPrimaryKey()]);
} catch (Throwable $e) {
throw new Exception();
}
foreach ($this->GetAllFieldNames() as $field) {
$this->fields[$field][self::VALUE] = null;
@ -157,7 +156,10 @@ abstract class Table
protected function setField(string $name, $value): void
{
if (!$this->HasField($name)) {
throw new Exception(sprintf('Field %s doesn\'t exist!', $name));
throw new DatabaseException(
sprintf('Field %s doesn\'t exist!', $name),
ServerStatus::INTERNAL_ERROR
);
}
if ($value === null) {
@ -178,8 +180,11 @@ abstract class Table
case self::TYPE_DATETIME:
try {
$this->fields[$name][self::VALUE] = new DateTime((string)$value);
} catch (Exception $e) {
throw new Exception();
} catch (Throwable $e) {
throw new DatabaseException(
$e->getMessage(),
ServerStatus::INTERNAL_ERROR
);
}
return;
case self::TYPE_BOOL:
@ -254,7 +259,10 @@ abstract class Table
protected function saveWithManualId(array $fields): void
{
if ($this->getField($this->primaryKey) === null) {
throw new Exception('Manual primary key must not be null!');
throw new DatabaseException(
'Manual primary key must not be null!',
ServerStatus::INTERNAL_ERROR
);
}
$hasKey = (bool)$this->database->Count(