From ca03e96fb9298c7a58375cb4005f5619b5d6545f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 28 Aug 2016 16:57:49 +0200 Subject: [PATCH] Started to implement better exceptions --- AutoloadException.php | 45 ++++++++++++++++++++++++++++ Autoloader.php | 2 +- DataStorage/Cookie/CookieJar.php | 4 ++- DataStorage/LockException.php | 45 ++++++++++++++++++++++++++++ DataStorage/Session/HttpSession.php | 5 ++-- Math/Matrix/DimensionException.php | 46 +++++++++++++++++++++++++++++ Math/Matrix/Matrix.php | 12 ++++---- 7 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 AutoloadException.php create mode 100644 DataStorage/LockException.php create mode 100644 Math/Matrix/DimensionException.php diff --git a/AutoloadException.php b/AutoloadException.php new file mode 100644 index 000000000..28b8322e0 --- /dev/null +++ b/AutoloadException.php @@ -0,0 +1,45 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS; + +/** + * Permission exception class. + * + * @category Framework + * @package phpOMS\System\File + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class AutoloadException extends \RuntimeException +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) + { + parent::__construct('File "' . $message . '" could not get loaded.', $code, $previous); + } +} diff --git a/Autoloader.php b/Autoloader.php index 4f4a5c55a..1adc9a825 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -50,7 +50,7 @@ class Autoloader /** @noinspection PhpIncludeInspection */ include __DIR__ . '/../' . $classNew . '.php'; } else { - throw new \Exception($class); + throw new AutoloaderException($class); } } diff --git a/DataStorage/Cookie/CookieJar.php b/DataStorage/Cookie/CookieJar.php index e0b610bcc..958c17235 100644 --- a/DataStorage/Cookie/CookieJar.php +++ b/DataStorage/Cookie/CookieJar.php @@ -16,6 +16,8 @@ namespace phpOMS\DataStorage\Cookie; +use phpOMS\DataStorage\LockException; + /** * CookieJar class * @@ -160,7 +162,7 @@ class CookieJar public function save() { if (self::$isLocked) { - throw new \Exception('Already locked'); + throw new LockException('CookieJar'); } foreach ($this->cookies as $key => $cookie) { diff --git a/DataStorage/LockException.php b/DataStorage/LockException.php new file mode 100644 index 000000000..a0dffcf53 --- /dev/null +++ b/DataStorage/LockException.php @@ -0,0 +1,45 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage; + +/** + * Permission exception class. + * + * @category Framework + * @package phpOMS\System\File + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class LockException extends \RuntimeException +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) + { + parent::__construct('Interaction with "' . $message . '" already locked.', $code, $previous); + } +} diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index ae0efef24..926439333 100644 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -17,6 +17,7 @@ namespace phpOMS\DataStorage\Session; use phpOMS\Uri\UriFactory; use phpOMS\Utils\RnG\StringUtils; +use phpOMS\DataStorage\LockException; /** * Http session class. @@ -62,7 +63,7 @@ class HttpSession implements SessionInterface * @param int $liftetime Session life time * @param string|int|bool $sid Session id * - * @throws \Exception Throws this exception if the session is alrady locked for further interaction. + * @throws LockException Throws this exception if the session is alrady locked for further interaction. * * @since 1.0.0 * @author Dennis Eichhorn @@ -70,7 +71,7 @@ class HttpSession implements SessionInterface public function __construct(int $liftetime = 3600, $sid = false) { if (self::$isLocked) { - throw new \Exception('Already locked'); + throw new LockException('HttpSession'); } if (!is_bool($sid)) { diff --git a/Math/Matrix/DimensionException.php b/Math/Matrix/DimensionException.php new file mode 100644 index 000000000..9ec74023a --- /dev/null +++ b/Math/Matrix/DimensionException.php @@ -0,0 +1,46 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Matrix; + +/** + * Permission exception class. + * + * @category Framework + * @package phpOMS\System\File + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class DimensionException extends \RuntimeException +{ + /** + * Constructor. + * + * @param int $m Dimension M + * @param int $n Dimension N + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct($m, $n, int $code = 0, \Exception $previous = null) + { + parent::__construct('Dimension of "' . $n . '-' . $m . '" invalid.', $code, $previous); + } +} diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 69cfae480..d45a42e0a 100644 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -95,7 +95,7 @@ class Matrix implements \ArrayAccess, \Iterator public function set(int $m, int $n, $value) { if (!isset($this->matrix[$m][$n])) { - throw new \Exception('Dimension'); + throw new DimensionException($m, $n); } $this->matrix[$m][$n] = $value; @@ -117,7 +117,7 @@ class Matrix implements \ArrayAccess, \Iterator public function get(int $m, int $n) { if (!isset($this->matrix[$m][$n])) { - throw new \Exception('Dimension'); + throw new DimensionException($m, $n); } return $this->matrix[$m][$n]; @@ -165,7 +165,7 @@ class Matrix implements \ArrayAccess, \Iterator public function setMatrix(array $matrix) { if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) { - throw new \Exception('Dimension'); + throw new DimensionException(count($matrix), count($matrix[0])); } $this->matrix = $matrix; @@ -232,7 +232,7 @@ class Matrix implements \ArrayAccess, \Iterator private function addMatrix(Matrix $matrix) : Matrix { if ($this->m !== $matrix->getM() || $this->n !== $matrix->getN()) { - throw new \Exception('Dimension'); + throw new DimensionException($matrix->getM(), $matrix->getN()); } $matrixArr = $matrix->getMatrix(); @@ -345,7 +345,7 @@ class Matrix implements \ArrayAccess, \Iterator $mDim = $matrix->getM(); if ($this->n !== $mDim) { - throw new \Exception('Dimension'); + throw new DimensionException($mDim, $nDim); } $matrixArr = $matrix->getMatrix(); @@ -496,7 +496,7 @@ class Matrix implements \ArrayAccess, \Iterator public function inverse(int $algorithm = InverseType::GAUSS_JORDAN) : Matrix { if ($this->n !== $this->m) { - throw new \Exception('Dimension'); + throw new DimensionException($this->m, $this->n); } switch ($algorithm) {