DBException.class.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace KIF\Exception;
  3. use Exception;
  4. /**
  5. *
  6. * 数据库异常类
  7. * @author gxg
  8. *
  9. */
  10. class DBException extends Exception {
  11. public function __construct ($message = 'DB异常', $code = 0) {
  12. parent::__construct($message, $code);
  13. }
  14. /**
  15. *
  16. * 未匹配到符合条件的记录
  17. * @var string
  18. */
  19. const NOT_MATCH = 'NOT_MATCH';
  20. /**
  21. *
  22. * 插入时已存在
  23. * @var string
  24. */
  25. const EXIST_ON_INSERT = 'EXIST_ON_INSERT';
  26. /**
  27. *
  28. * cas_token 不匹配
  29. * @var string
  30. */
  31. const CAS_TOKEN_NOT_MATCH = 'CAS_TOKEN_NOT_MATCH';
  32. /**
  33. *
  34. * 提交事务失败
  35. * @var string
  36. */
  37. const COMMIT_TRANSACTION_FAIL = 'COMMIT_TRANSACTION_FAIL';
  38. /**
  39. *
  40. * 获取异常描述
  41. * @return array
  42. */
  43. static public function getDesc() {
  44. return array(
  45. self::NOT_MATCH => array(
  46. 'kw' => 'NOT_MATCH',
  47. 'desc' => '未匹配到符合条件的记录',
  48. ),
  49. self::CAS_TOKEN_NOT_MATCH => array(
  50. 'kw' => 'CAS_TOKEN_NOT_MATCH',
  51. 'desc' => 'cas_token不匹配',
  52. ),
  53. self::COMMIT_TRANSACTION_FAIL => array(
  54. 'kw' => 'COMMIT_TRANSACTION_FAIL',
  55. 'desc' => '提交事务失败',
  56. ),
  57. );
  58. }
  59. /**
  60. *
  61. * 根据异常的关键词获取文本型的描述
  62. * @param string $kw
  63. * @return false | string
  64. */
  65. static public function getTextDescByKW($kw) {
  66. $descs = self::getDesc();
  67. if (isset($descs[$kw])) {
  68. return $descs[$kw]['desc'];
  69. }
  70. return false;
  71. }
  72. }