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