PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/dibi/libs/DibiException.php

https://github.com/bazo/Mokuji
PHP | 163 lines | 55 code | 45 blank | 63 comment | 1 complexity | ec543fb748661f0dd513b061f983fc33 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * dibi - tiny'n'smart database abstraction layer
  4. * ----------------------------------------------
  5. *
  6. * @copyright Copyright (c) 2005, 2010 David Grudl
  7. * @license http://dibiphp.com/license dibi license
  8. * @link http://dibiphp.com
  9. * @package dibi
  10. */
  11. /**
  12. * dibi common exception.
  13. *
  14. * @copyright Copyright (c) 2005, 2010 David Grudl
  15. * @package dibi
  16. */
  17. class DibiException extends Exception implements /*Nette\*/IDebugPanel
  18. {
  19. /** @var string */
  20. private $sql;
  21. /**
  22. * Construct a dibi exception.
  23. * @param string Message describing the exception
  24. * @param int Some code
  25. * @param string SQL command
  26. */
  27. public function __construct($message = NULL, $code = 0, $sql = NULL)
  28. {
  29. parent::__construct($message, (int) $code);
  30. $this->sql = $sql;
  31. // TODO: add $profiler->exception($this);
  32. }
  33. /**
  34. * @return string The SQL passed to the constructor
  35. */
  36. final public function getSql()
  37. {
  38. return $this->sql;
  39. }
  40. /**
  41. * @return string string represenation of exception with SQL command
  42. */
  43. public function __toString()
  44. {
  45. return parent::__toString() . ($this->sql ? "\nSQL: " . $this->sql : '');
  46. }
  47. /********************* interface Nette\IDebugPanel ****************d*g**/
  48. /**
  49. * Returns HTML code for custom tab.
  50. * @return mixed
  51. */
  52. public function getTab()
  53. {
  54. return 'SQL';
  55. }
  56. /**
  57. * Returns HTML code for custom panel.
  58. * @return mixed
  59. */
  60. public function getPanel()
  61. {
  62. return $this->sql ? dibi::dump($this->sql, TRUE) : NULL;
  63. }
  64. /**
  65. * Returns panel ID.
  66. * @return string
  67. */
  68. public function getId()
  69. {
  70. return __CLASS__;
  71. }
  72. }
  73. /**
  74. * database server exception.
  75. *
  76. * @copyright Copyright (c) 2005, 2010 David Grudl
  77. * @package dibi
  78. */
  79. class DibiDriverException extends DibiException
  80. {
  81. /********************* error catching ****************d*g**/
  82. /** @var string */
  83. private static $errorMsg;
  84. /**
  85. * Starts catching potential errors/warnings.
  86. * @return void
  87. */
  88. public static function tryError()
  89. {
  90. set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL);
  91. self::$errorMsg = NULL;
  92. }
  93. /**
  94. * Returns catched error/warning message.
  95. * @param string catched message
  96. * @return bool
  97. */
  98. public static function catchError(& $message)
  99. {
  100. restore_error_handler();
  101. $message = self::$errorMsg;
  102. self::$errorMsg = NULL;
  103. return $message !== NULL;
  104. }
  105. /**
  106. * Internal error handler. Do not call directly.
  107. * @internal
  108. */
  109. public static function _errorHandler($code, $message)
  110. {
  111. restore_error_handler();
  112. if (ini_get('html_errors')) {
  113. $message = strip_tags($message);
  114. $message = html_entity_decode($message);
  115. }
  116. self::$errorMsg = $message;
  117. }
  118. }