From 692a4a48d5f8f74a06d0b5890e31887a76a903f3 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Sat, 8 Jul 2017 13:58:36 -0400 Subject: Improve exception handling, introduce autoloading --- private/Controller/Controller.php | 44 +++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'private/Controller') 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 @@ -11,6 +11,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. * @@ -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())); } } -- cgit