Skip to content

Commit 6e044f1

Browse files
author
ethan
committed
Exception
1 parent 1ad02e5 commit 6e044f1

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

src/Builder.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PFinal\Database;
44

55
use Closure;
6-
use Throwable;
76

87
/**
98
* 数据库操作辅助类
@@ -88,7 +87,6 @@ public function getConnection()
8887
*
8988
* @param string $tableName 支持 as 例如 user as u
9089
* @return static
91-
* @throws Exception
9290
*/
9391
public function table($tableName = '')
9492
{
@@ -125,7 +123,7 @@ public function addPrefix($tableName)
125123
$tableName = '{{%' . $tableName . '}}';
126124
}
127125
if (!preg_match('/^\{\{%?[\w\-\.\$]+%?\}\}$/', $tableName)) {
128-
throw new Exception('表名错误');
126+
throw new Exception('表名含有不被允许的字符');
129127
}
130128
return $tableName . $asName;
131129
}
@@ -170,7 +168,6 @@ public function insertGetId(array $data)
170168
* @param string $sql
171169
* @param array $params
172170
* @return array
173-
* @throws Exception
174171
*/
175172
public function findAllBySql($sql = '', $params = array())
176173
{
@@ -401,7 +398,7 @@ public function findByPkOrFail($id, $primaryKeyField = null)
401398
{
402399
$data = static::findByPk($id, $primaryKeyField);
403400
if ($data == null) {
404-
throw new NotFoundException();
401+
throw new NotFoundException("Data not found: #" . $id);
405402
}
406403
return $data;
407404
}
@@ -846,7 +843,7 @@ public function field($field)
846843
* @param int $attempts 事务会重试的次数。如果重试结束还没有成功执行,将会抛出一个异常
847844
* @return mixed
848845
*
849-
* @throws \Exception|\Throwable
846+
* @throws \Throwable
850847
*/
851848
public function transaction(Closure $callback, $attempts = 1)
852849
{
@@ -859,12 +856,12 @@ public function transaction(Closure $callback, $attempts = 1)
859856
$result = $callback($this);
860857
$this->getConnection()->commit();
861858

862-
} catch (Exception $e) {
859+
} catch (\Exception $e) {
863860

864861
$this->getConnection()->rollBack();
865862
throw $e; //回滚事务后继续向外抛出异常,让开发人员自行处理后续操作
866863

867-
} catch (Throwable $e) { //PHP 7
864+
} catch (\Throwable $e) { //PHP 7
868865

869866
$this->getConnection()->rollBack();
870867
throw $e;
@@ -1035,7 +1032,7 @@ protected function getFieldString()
10351032
$return = $field;
10361033
}
10371034
if (!preg_match('/^[\w\s\.\,\[\]`\*]+$/', $return)) {
1038-
throw new Exception(__CLASS__ . '::field() 含有不安全的字符');//字母、数字、下划线、空白、点、星号、逗号、中括号、反引号
1035+
throw new Exception('字段名含有不被允许的字符');//字母、数字、下划线、空白、点、星号、逗号、中括号、反引号
10391036
}
10401037
}
10411038
return $return;
@@ -1091,7 +1088,7 @@ protected function getLimitString()
10911088
return ' LIMIT ' . $offset . ', ' . $limit;
10921089
}
10931090
}
1094-
throw new Exception("offset or limit 包含非法字符");
1091+
throw new Exception("offset limit 含有不被允许的字符");
10951092
}
10961093

10971094
/**
@@ -1120,7 +1117,7 @@ protected function appendLock($sql)
11201117
protected static function checkColumnName($column)
11211118
{
11221119
if (!preg_match('/^[\w\-\.]+$/', $column)) {
1123-
throw new Exception('列名只允许字母、数字、下划线、点(.)、中杠(-)');
1120+
throw new Exception('列名含有不被允许的字符');//只允许字母、数字、下划线、点(.)、中杠(-)
11241121
}
11251122
}
11261123

@@ -1180,7 +1177,7 @@ public function useWritePdo()
11801177
public function afterFind($callback)
11811178
{
11821179
if (!is_callable($callback)) {
1183-
throw new Exception('After find must callable');
1180+
throw new Exception('$callback is not a callable');
11841181
}
11851182
$this->afterFind = $callback;
11861183

src/Exception.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PFinal\Database;
44

5-
class Exception extends \RuntimeException
5+
class Exception extends \LogicException
66
{
7-
public function __construct($message = "Unknown database exception", $code = 0, $previous = null)
7+
public function __construct($message, $code = 0, $previous = null)
88
{
99
parent::__construct($message, $code, $previous);
1010
}

src/NotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PFinal\Database;
44

5-
class NotFoundException extends Exception
5+
class NotFoundException extends \RuntimeException
66
{
7-
public function __construct($message = "Data not found exception", $code = 0, $previous = null)
7+
public function __construct($message = "Data not found.", $code = 0, $previous = null)
88
{
99
parent::__construct($message, $code, $previous);
1010
}

0 commit comments

Comments
 (0)