暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PDOException.php 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace think\exception;
  12. use think\Config;
  13. /**
  14. * PDO异常处理类
  15. * 重新封装了系统的\PDOException类
  16. */
  17. class PDOException extends DbException
  18. {
  19. /**
  20. * PDOException constructor.
  21. * @param \PDOException $exception
  22. * @param array $config
  23. * @param string $sql
  24. * @param int $code
  25. */
  26. public function __construct(\PDOException $exception, array $config, $sql, $code = 10501)
  27. {
  28. $error = $exception->errorInfo;
  29. $code0 = $exception->getCode();
  30. $code1 = isset($error[1]) ? $error[1] : 0;
  31. $code2 = isset($error[2]) ? $error[2] : '';
  32. $this->setData('PDO Error Info', [
  33. 'SQLSTATE' => $code0,
  34. 'Driver Error Code' => $code1,
  35. 'Driver Error Message' => $code2,
  36. ]);
  37. /*提高错误提示的友好性 by 小虎哥*/
  38. $errcode = "{$code0}:{$code1}";
  39. if (stristr($code2, "Incorrect string value:")) {
  40. if (stristr($code2, "for column '")) {
  41. $errcode = "HY000:-{$code1}";
  42. }
  43. }
  44. $mysqlcode = Config::get('error_code.mysql');
  45. $eyou_message = "";
  46. if (!empty($mysqlcode[$errcode])) {
  47. $code = 'eyou';
  48. $eyou_message = $mysqlcode[$errcode];
  49. }
  50. /*--end*/
  51. $message = $exception->getMessage();
  52. try {
  53. $message = iconv('GB2312', 'UTF-8', $message); // 转化编码 by 小虎哥
  54. } catch (\Exception $e) {
  55. if (function_exists('mb_convert_encoding')) {
  56. $message = mb_convert_encoding($message, "UTF-8", "GBK");
  57. }
  58. }
  59. // 新增/更新时,字段超过长度报错
  60. if (stristr($message, 'Data too long for column ')) {
  61. $table = '';
  62. if (preg_match('/^INSERT(\s+)INTO(\s+)`([\w\-]+)`(\s+)(.*)$/i', $sql)) {
  63. $table = preg_replace('/^INSERT(\s+)INTO(\s+)`([\w\-]+)`(\s+)(.*)$/i', '${3}', $sql);
  64. } else if (preg_match('/^UPDATE(\s+)`([\w\-]+)`(\s+)SET(\s+)`(.*)$/i', $sql)) {
  65. $table = preg_replace('/^UPDATE(\s+)`([\w\-]+)`(\s+)SET(\s+)`(.*)$/i', '${2}', $sql);
  66. }
  67. if (!empty($table) && !stristr($table, '`')) {
  68. $data = \think\Db::query("show create table " . $table);
  69. $sqlInfo = empty($data[0]['Create Table']) ? '' : $data[0]['Create Table'];
  70. if (!empty($sqlInfo)) {
  71. $fieldName = preg_replace('/^(.*)Data too long for column (\'|")([^\'\"]+)(\'|") at row (.*)$/i', '${3}', $message);
  72. $fieldTitle = preg_replace('/^([\s\S]+)`'.$fieldName.'`(\s+)(.*)(\s+)COMMENT(\s+)(\'|")(.*)(\'|")\,([\s\S]+)$/i', '${7}', $sqlInfo);
  73. $fieldLength = preg_replace('/^([\s\S]+)`'.$fieldName.'`(\s+)([a-z]+)\((\d+)([\s\S]+)$/i', '${4}', $sqlInfo);
  74. $eyou_message = $fieldTitle.$eyou_message.$fieldLength.'字符';
  75. }
  76. }
  77. }
  78. $message = $eyou_message."#--wrap--#".$message;
  79. parent::__construct($message, $config, $sql, $code);
  80. }
  81. }