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 { try {
$this->connection = new PDO("mysql:host=$hostname;dbname=$database", $user, $password); $this->connection = new PDO("mysql:host=$hostname;dbname=$database", $user, $password);
} catch (Throwable $e) { } 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 { try {
$this->Query($query, $conditions); $this->Query($query, $conditions);
} catch (Throwable $e) { } catch (Throwable $e) {
return []; throw new DatabaseException(
$e->getMessage(),
ServerStatus::INTERNAL_ERROR
);
} }
return $this->getResult(); return $this->getResult();

View File

@ -67,7 +67,10 @@ abstract class Table
$type = self::TYPE_BOOL; $type = self::TYPE_BOOL;
break; break;
default: 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); $this->addField($field['Field'], $type);
@ -94,8 +97,9 @@ abstract class Table
protected function addField(string $name, int $type): void protected function addField(string $name, int $type): void
{ {
if (!self::IsValidType($type)) { if (!self::IsValidType($type)) {
throw new Exception( throw new DatabaseException(
sprintf('Field %s has invalid type of %s!', $name, $type) 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 protected function loadById($id): void
{ {
try { $this->database->Query(
$this->database->Query( sprintf('SELECT * FROM %s WHERE %s = :id', $this->tableName, $this->primaryKey),
sprintf('SELECT * FROM %s WHERE %s = :id', $this->tableName, $this->primaryKey), ['id' => $id]
['id' => $id] );
);
} catch (Throwable $e) {
throw new Exception();
}
$result = $this->database->getResult(); $result = $this->database->getResult();
if (count($result) === 0) { 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) { foreach ($result[0] as $field => $value) {
@ -131,11 +134,7 @@ abstract class Table
public function Delete(): void public function Delete(): void
{ {
try { $this->database->Delete($this->tableName, [$this->primaryKey => $this->getPrimaryKey()]);
$this->database->Delete($this->tableName, [$this->primaryKey => $this->getPrimaryKey()]);
} catch (Throwable $e) {
throw new Exception();
}
foreach ($this->GetAllFieldNames() as $field) { foreach ($this->GetAllFieldNames() as $field) {
$this->fields[$field][self::VALUE] = null; $this->fields[$field][self::VALUE] = null;
@ -157,7 +156,10 @@ abstract class Table
protected function setField(string $name, $value): void protected function setField(string $name, $value): void
{ {
if (!$this->HasField($name)) { 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) { if ($value === null) {
@ -178,8 +180,11 @@ abstract class Table
case self::TYPE_DATETIME: case self::TYPE_DATETIME:
try { try {
$this->fields[$name][self::VALUE] = new DateTime((string)$value); $this->fields[$name][self::VALUE] = new DateTime((string)$value);
} catch (Exception $e) { } catch (Throwable $e) {
throw new Exception(); throw new DatabaseException(
$e->getMessage(),
ServerStatus::INTERNAL_ERROR
);
} }
return; return;
case self::TYPE_BOOL: case self::TYPE_BOOL:
@ -254,7 +259,10 @@ abstract class Table
protected function saveWithManualId(array $fields): void protected function saveWithManualId(array $fields): void
{ {
if ($this->getField($this->primaryKey) === null) { 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( $hasKey = (bool)$this->database->Count(