diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2017-07-08 13:58:36 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2017-07-08 13:58:36 -0400 |
| commit | 692a4a48d5f8f74a06d0b5890e31887a76a903f3 (patch) | |
| tree | 9fc39dc20be3b8a442031b85beffc7baa583143b /private/Controller/Controller.php | |
| parent | 0364d790e9764d91489db21805064ceeb23e3e22 (diff) | |
| download | august-offensive-692a4a48d5f8f74a06d0b5890e31887a76a903f3.tar.gz august-offensive-692a4a48d5f8f74a06d0b5890e31887a76a903f3.tar.bz2 august-offensive-692a4a48d5f8f74a06d0b5890e31887a76a903f3.zip | |
Improve exception handling, introduce autoloading
Diffstat (limited to 'private/Controller/Controller.php')
| -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())); } } |
