Source for file DibiException.php

Documentation is available at DibiException.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * dibi - tiny'n'smart database abstraction layer
  5. 5:  * ----------------------------------------------
  6. 6:  *
  7. 7:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  8. 8:  * @license    http://dibiphp.com/license  dibi license
  9. 9:  * @link       http://dibiphp.com
  10. 10:  * @package    dibi
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * dibi common exception.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  19. 19:  * @package    dibi
  20. 20:  */
  21. 21: class DibiException extends Exception implements IDebugPanel
  22. 22: {
  23. 23:     /** @var string */
  24. 24:     private $sql;
  25. 25:  
  26. 26:  
  27. 27:     /**
  28. 28:      * Construct a dibi exception.
  29. 29:      * @param  string  Message describing the exception
  30. 30:      * @param  int     Some code
  31. 31:      * @param  string SQL command
  32. 32:      */
  33. 33:     public function __construct($message NULL$code 0$sql NULL)
  34. 34:     {
  35. 35:         parent::__construct($message(int) $code);
  36. 36:         $this->sql $sql;
  37. 37:         // TODO: add $profiler->exception($this);
  38. 38:     }
  39. 39:  
  40. 40:  
  41. 41:  
  42. 42:     /**
  43. 43:      * @return string  The SQL passed to the constructor
  44. 44:      */
  45. 45:     final public function getSql()
  46. 46:     {
  47. 47:         return $this->sql;
  48. 48:     }
  49. 49:  
  50. 50:  
  51. 51:  
  52. 52:     /**
  53. 53:      * @return string  string represenation of exception with SQL command
  54. 54:      */
  55. 55:     public function __toString()
  56. 56:     {
  57. 57:         return parent::__toString(($this->sql "\nSQL: " $this->sql '');
  58. 58:     }
  59. 59:  
  60. 60:  
  61. 61:  
  62. 62:     /********************* interface Nette\IDebugPanel ****************d*g**/
  63. 63:  
  64. 64:  
  65. 65:  
  66. 66:     /**
  67. 67:      * Returns HTML code for custom tab.
  68. 68:      * @return mixed 
  69. 69:      */
  70. 70:     public function getTab()
  71. 71:     {
  72. 72:         return 'SQL';
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Returns HTML code for custom panel.
  79. 79:      * @return mixed 
  80. 80:      */
  81. 81:     public function getPanel()
  82. 82:     {
  83. 83:         return $this->sql dibi::dump($this->sqlTRUENULL;
  84. 84:     }
  85. 85:  
  86. 86:  
  87. 87:  
  88. 88:     /**
  89. 89:      * Returns panel ID.
  90. 90:      * @return string 
  91. 91:      */
  92. 92:     public function getId()
  93. 93:     {
  94. 94:         return __CLASS__;
  95. 95:     }
  96. 96:  
  97. 97: }
  98. 98:  
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102: /**
  103. 103:  * database server exception.
  104. 104:  *
  105. 105:  * @copyright  Copyright (c) 2005, 2010 David Grudl
  106. 106:  * @package    dibi
  107. 107:  */
  108. 110:  
  109. 111:     /********************* error catching ****************d*g**/
  110. 112:  
  111. 113:  
  112. 114:  
  113. 115:     /** @var string */
  114. 116:     private static $errorMsg;
  115. 117:  
  116. 118:  
  117. 119:  
  118. 120:     /**
  119. 121:      * Starts catching potential errors/warnings.
  120. 122:      * @return void 
  121. 123:      */
  122. 124:     public static function tryError()
  123. 125:     {
  124. 126:         set_error_handler(array(__CLASS__'_errorHandler')E_ALL);
  125. 127:         self::$errorMsg NULL;
  126. 128:     }
  127. 129:  
  128. 130:  
  129. 131:  
  130. 132:     /**
  131. 133:      * Returns catched error/warning message.
  132. 134:      * @param  string  catched message
  133. 135:      * @return bool 
  134. 136:      */
  135. 137:     public static function catchError($message)
  136. 138:     {
  137. 139:         restore_error_handler();
  138. 140:         $message self::$errorMsg;
  139. 141:         self::$errorMsg NULL;
  140. 142:         return $message !== NULL;
  141. 143:     }
  142. 144:  
  143. 145:  
  144. 146:  
  145. 147:     /**
  146. 148:      * Internal error handler. Do not call directly.
  147. 149:      * @ignore internal
  148. 150:      */
  149. 151:     public static function _errorHandler($code$message)
  150. 152:     {
  151. 153:         restore_error_handler();
  152. 154:  
  153. 155:         if (ini_get('html_errors')) {
  154. 156:             $message strip_tags($message);
  155. 157:             $message html_entity_decode($message);
  156. 158:         }
  157. 159:  
  158. 160:         self::$errorMsg $message;
  159. 161:     }
  160. 162: