Added custom exceptions

This commit is contained in:
Dennis Eichhorn 2017-07-25 17:29:23 +02:00
parent 939253aa95
commit 33849c8a1f
4 changed files with 107 additions and 13 deletions

View File

@ -20,6 +20,7 @@ use phpOMS\DataStorage\Database\DatabaseStatus;
use phpOMS\DataStorage\Database\DatabaseType;
use phpOMS\DataStorage\Database\Query\Grammar\MysqlGrammar;
use phpOMS\DataStorage\Database\Schema\Grammar\MysqlGrammar as MysqlSchemaGrammar;
use phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException;
/**
* Database handler.
@ -62,27 +63,27 @@ class MysqlConnection extends ConnectionAbstract
$this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata;
if(!isset($this->dbdata['db'])) {
throw new \Exception('db');
throw new InvalidConnectionConfigException('db');
}
if(!isset($this->dbdata['host'])) {
throw new \Exception('host');
throw new InvalidConnectionConfigException('host');
}
if(!isset($this->dbdata['port'])) {
throw new \Exception('port');
throw new InvalidConnectionConfigException('port');
}
if(!isset($this->dbdata['database'])) {
throw new \Exception('database');
throw new InvalidConnectionConfigException('database');
}
if(!isset($this->dbdata['login'])) {
throw new \Exception('login');
throw new InvalidConnectionConfigException('login');
}
if(!isset($this->dbdata['password'])) {
throw new \Exception('password');
throw new InvalidConnectionConfigException('password');
}
$this->close();

View File

@ -20,6 +20,7 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\DataMapperInterface;
use phpOMS\Message\RequestAbstract;
use phpOMS\DataStorage\Database\Exception\InvalidMapperException;
/**
* Datamapper for databases.
@ -460,7 +461,7 @@ class DataMapperAbstract implements DataMapperInterface
*
* @return void
*
* @throws \Exception
* @throws InvalidMapperException
*
* @since 1.0.0
*/
@ -480,7 +481,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new \Exception('No mapper set for relation object.');
throw new InvalidMapperException();
}
/** @var DataMapperAbstract $mapper */
@ -679,7 +680,7 @@ class DataMapperAbstract implements DataMapperInterface
*
* @return void
*
* @throws \Exception
* @throws InvalidMapperException
*
* @since 1.0.0
*/
@ -699,7 +700,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new \Exception('No mapper set for relation object.');
throw new InvalidMapperException();
}
/** @var DataMapperAbstract $mapper */
@ -984,7 +985,7 @@ class DataMapperAbstract implements DataMapperInterface
*
* @return void
*
* @throws \Exception
* @throws InvalidMapperException
*
* @since 1.0.0
*/
@ -1004,7 +1005,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new \Exception('No mapper set for relation object.');
throw new InvalidMapperException();
}
/** @var DataMapperAbstract $mapper */
@ -1406,7 +1407,7 @@ class DataMapperAbstract implements DataMapperInterface
*
* @return mixed
*
* @throws \Exception
* @throws \UnexpectedValueException
*
* @since 1.0.0
*/

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Exception;
/**
* Permission exception class.
*
* @category Framework
* @package phpOMS\System\File
* @author OMS Development Team <dev@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class InvalidConnectionConfigException extends \RuntimeException
{
/**
* Constructor.
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
*
* @since 1.0.0
*/
public function __construct(string $message = '', int $code = 0, \Exception $previous = null)
{
parent::__construct('Missing config value for "'. $message .'".', $code, $previous);
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database\Exception;
/**
* Permission exception class.
*
* @category Framework
* @package phpOMS\System\File
* @author OMS Development Team <dev@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class InvalidMapperException extends \RuntimeException
{
/**
* Constructor.
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
*
* @since 1.0.0
*/
public function __construct(string $message = '', int $code = 0, \Exception $previous = null)
{
if($message === '') {
parent::__construct('Empty mapper.', $code, $previous);
} else {
parent::__construct('Mapper "' . $message . '" is invalid.', $code, $previous);
}
}
}