diff options
Diffstat (limited to 'private/Controller')
| -rw-r--r-- | private/Controller/Controller.php | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/private/Controller/Controller.php b/private/Controller/Controller.php index 188345a..586be15 100644 --- a/private/Controller/Controller.php +++ b/private/Controller/Controller.php @@ -12,6 +12,20 @@ use AugustOffensive\Model; class Controller { /** + * Initiates connection with the database. + * + * While errors are ideally handled by the controller, this instantiation + * will likely throw a \PDOException which should be handled by the front- + * end due to this being a fatal error and generally unrecoverable. + * + * @return Model\Connection + */ + public static function initiateConnection (): Model\Connection + { + return new Model\Connection(); + } + + /** * Creates and returns a Query object. * * If the creation results in an error, a different query object is @@ -29,9 +43,9 @@ class Controller array $content ): Model\Query { try { - return new Model\Query ($path, $request, $content); + return new Model\Query($path, $request, $content); } catch (\Exception $err) { - return new Model\Query (array(), "", array("ERROR" => $err->getMessage())); + return new Model\Query(array(), "", array("ERROR" => $err->getMessage())); } } @@ -48,9 +62,31 @@ class Controller array $result ): Model\Result { try { - return new Model\Result ($resultType, $result); + return new Model\Result($resultType, $result); } catch (\Exception $err) { - return new Model\Result ("ERROR", array($err->getMessage())); + return new Model\Result("ERROR", array($err->getMessage())); + } + } + + /** + * Obtain the error result based on the exception that was thrown. + * + * @param \Exception $err the error that was thrown. + * + * @return Model\Result + */ + public static function errorResult (\Exception $err): Model\Result + { + $errorType = ""; + // Juggle error: objective is to sort error type + try { + throw $err; + } catch (\PDOException $e) { + $errorType = "DATABASE_ERROR"; + } catch (\Exception $e) { + $errorType = "ERROR"; } + + return new Model\Result($errorType, array("error" => $err->getMessage())); } } |
