From 3a99e99130b511c0f00148d885d665c56f501148 Mon Sep 17 00:00:00 2001 From: Mal <=> Date: Fri, 21 Aug 2020 22:46:19 +0200 Subject: [PATCH] Error handling simplified --- backend/classes/core/MySqlDatabase.php | 10 ++++-- backend/classes/core/Table.php | 50 +++++++++++++++----------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/backend/classes/core/MySqlDatabase.php b/backend/classes/core/MySqlDatabase.php index 6458739..5e8d119 100644 --- a/backend/classes/core/MySqlDatabase.php +++ b/backend/classes/core/MySqlDatabase.php @@ -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(); diff --git a/backend/classes/core/Table.php b/backend/classes/core/Table.php index 0641608..a56fd46 100644 --- a/backend/classes/core/Table.php +++ b/backend/classes/core/Table.php @@ -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(); - } + $this->database->Query( + sprintf('SELECT * FROM %s WHERE %s = :id', $this->tableName, $this->primaryKey), + ['id' => $id] + ); $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(); - } + $this->database->Delete($this->tableName, [$this->primaryKey => $this->getPrimaryKey()]); 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(