From 5cbce8ea74e3c986073473c96b766f8178fad48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Mon, 1 Feb 2016 00:03:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0DB=E5=BC=82=E5=B8=B8=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think/exception/DbBindParamException.php | 26 ++++++++++++++ library/think/exception/DbException.php | 11 +++--- library/think/exception/PDOException.php | 34 +++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 library/think/exception/DbBindParamException.php create mode 100644 library/think/exception/PDOException.php diff --git a/library/think/exception/DbBindParamException.php b/library/think/exception/DbBindParamException.php new file mode 100644 index 00000000..21ddcd58 --- /dev/null +++ b/library/think/exception/DbBindParamException.php @@ -0,0 +1,26 @@ + +// +---------------------------------------------------------------------- + +namespace think\exception; + +use think\exception\DbException; + +/** + * PDO参数绑定异常 + */ +class DbBindParamException extends DbException +{ + public function __construct($message, $config, $sql, $bind, $code = 10502) + { + $this->setData('Bind Param', $bind); + parent::__construct($message, $config, $sql, $code); + } +} diff --git a/library/think/exception/DbException.php b/library/think/exception/DbException.php index a816ad6a..1a14899e 100644 --- a/library/think/exception/DbException.php +++ b/library/think/exception/DbException.php @@ -18,18 +18,19 @@ use think\Exception; */ class DbException extends Exception { - public function __construct($message, $code, $db, $config) + public function __construct($message, Array $config, $sql, $code = 10500) { $this->message = $message; $this->code = $code; - $error = explode(':', $db->getError()); $this->setData('Database Status', [ - 'Error Code' => $error[0], - 'Error Message' => $error[1], - 'Error SQL' => $db->getLastSql() + 'Error Code' => $code, + 'Error Message' => $message, + 'Error SQL' => $sql ]); $this->setData('Database Config', $config); } + + } diff --git a/library/think/exception/PDOException.php b/library/think/exception/PDOException.php new file mode 100644 index 00000000..971b1c54 --- /dev/null +++ b/library/think/exception/PDOException.php @@ -0,0 +1,34 @@ + +// +---------------------------------------------------------------------- + +namespace think\exception; + +use think\exception\DbException; + +/** + * PDO异常处理类 + * 重新封装了系统的\PDOException类 + */ +class PDOException extends DbException +{ + public function __construct(\PDOException $exception, Array $config, $sql, $code = 10501) + { + $error = $exception->errorInfo; + + $this->setData('PDO Error Info', [ + 'SQLSTATE' => $error[0], + 'Driver Error Code' => $error[1], + 'Driver Error Message' => isset($error[2]) ? $error[2] : '' + ]); + + parent::__construct($exception->getMessage(), $config, $sql, $code); + } +}