2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
8 * LICENSE: This source file is subject to version 3.0 of the PHP license
9 * that is available through the world-wide-web at the following URI:
10 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
11 * the PHP License and are unable to obtain it through the web, please
12 * send a note to license@php.net so we can mail you a copy immediately.
16 * @author Tomas V. V. Cox <cox@idecnet.com>
17 * @author Hans Lellelid <hans@velum.net>
18 * @author Bertrand Mansion <bmansion@mamasam.com>
19 * @author Greg Beaver <cellog@php.net>
20 * @copyright 1997-2008 The PHP Group
21 * @license http://www.php.net/license/3_0.txt PHP License 3.0
22 * @version CVS: $Id: Exception.php,v 1.29 2008/01/03 20:26:35 cellog Exp $
23 * @link http://pear.php.net/package/PEAR
24 * @since File available since Release 1.3.3
29 * Base PEAR_Exception Class
33 * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
34 * - Definable triggers, shot when exceptions occur
35 * - Pretty and informative error messages
36 * - Added more context info available (like class, method or cause)
37 * - cause can be a PEAR_Exception or an array of mixed
38 * PEAR_Exceptions/PEAR_ErrorStack warnings
39 * - callbacks for specific exception classes and their children
43 * - Maybe a way to define a 'template' for the output
45 * 3) Inherited properties from PHP Exception Class:
53 * 4) Inherited methods from PHP Exception Class:
62 * getTraceSafeAsString
68 * require_once 'PEAR/Exception.php';
72 * throw new PEAR_Exception('Error Message', ERROR_CODE);
76 * function myLogger($pear_exception) {
77 * echo $pear_exception->getMessage();
79 * // each time a exception is thrown the 'myLogger' will be called
80 * // (its use is completely optional)
81 * PEAR_Exception::addObserver('myLogger');
85 * } catch (PEAR_Exception $e) {
92 * @author Tomas V.V.Cox <cox@idecnet.com>
93 * @author Hans Lellelid <hans@velum.net>
94 * @author Bertrand Mansion <bmansion@mamasam.com>
95 * @author Greg Beaver <cellog@php.net>
96 * @copyright 1997-2008 The PHP Group
97 * @license http://www.php.net/license/3_0.txt PHP License 3.0
98 * @version Release: 1.7.2
99 * @link http://pear.php.net/package/PEAR
100 * @since Class available since Release 1.3.3
103 class PEAR_Exception extends Exception
105 const OBSERVER_PRINT = -2;
106 const OBSERVER_TRIGGER = -4;
107 const OBSERVER_DIE = -8;
109 private static $_observers = array();
110 private static $_uniqueid = 0;
114 * Supported signatures:
115 * - PEAR_Exception(string $message);
116 * - PEAR_Exception(string $message, int $code);
117 * - PEAR_Exception(string $message, Exception $cause);
118 * - PEAR_Exception(string $message, Exception $cause, int $code);
119 * - PEAR_Exception(string $message, PEAR_Error $cause);
120 * - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
121 * - PEAR_Exception(string $message, array $causes);
122 * - PEAR_Exception(string $message, array $causes, int $code);
123 * @param string exception message
124 * @param int|Exception|PEAR_Error|array|null exception cause
125 * @param int|null exception code or null
127 public function __construct($message, $p2 = null, $p3 = null)
132 } elseif (is_object($p2) || is_array($p2)) {
133 // using is_object allows both Exception and PEAR_Error
134 if (is_object($p2) && !($p2 instanceof Exception)) {
135 if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
136 throw new PEAR_Exception('exception cause must be Exception, ' .
137 'array, or PEAR_Error');
141 if (is_array($p2) && isset($p2['message'])) {
142 // fix potential problem of passing in a single warning
150 parent::__construct($message, $code);
155 * @param mixed $callback - A valid php callback, see php func is_callable()
156 * - A PEAR_Exception::OBSERVER_* constant
157 * - An array(const PEAR_Exception::OBSERVER_*,
159 * @param string $label The name of the observer. Use this if you want
160 * to remove it later with removeObserver()
162 public static function addObserver($callback, $label = 'default')
164 self::$_observers[$label] = $callback;
167 public static function removeObserver($label = 'default')
169 unset(self::$_observers[$label]);
173 * @return int unique identifier for an observer
175 public static function getUniqueId()
177 return self::$_uniqueid++;
180 private function signal()
182 foreach (self::$_observers as $func) {
183 if (is_callable($func)) {
184 call_user_func($func, $this);
187 settype($func, 'array');
189 case self::OBSERVER_PRINT :
190 $f = (isset($func[1])) ? $func[1] : '%s';
191 printf($f, $this->getMessage());
193 case self::OBSERVER_TRIGGER :
194 $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
195 trigger_error($this->getMessage(), $f);
197 case self::OBSERVER_DIE :
198 $f = (isset($func[1])) ? $func[1] : '%s';
199 die(printf($f, $this->getMessage()));
202 trigger_error('invalid observer type', E_USER_WARNING);
208 * Return specific error information that can be used for more detailed
209 * error messages or translation.
211 * This method may be overridden in child exception classes in order
212 * to add functionality not present in PEAR_Exception and is a placeholder
215 * The returned array must be an associative array of parameter => value like so:
217 * array('name' => $name, 'context' => array(...))
221 public function getErrorData()
227 * Returns the exception that caused this exception to be thrown
229 * @return Exception|array The context of the exception
231 public function getCause()
237 * Function must be public to call on caused exceptions
240 public function getCauseMessage(&$causes)
242 $trace = $this->getTraceSafe();
243 $cause = array('class' => get_class($this),
244 'message' => $this->message,
246 'line' => 'unknown');
247 if (isset($trace[0])) {
248 if (isset($trace[0]['file'])) {
249 $cause['file'] = $trace[0]['file'];
250 $cause['line'] = $trace[0]['line'];
254 if ($this->cause instanceof PEAR_Exception) {
255 $this->cause->getCauseMessage($causes);
256 } elseif ($this->cause instanceof Exception) {
257 $causes[] = array('class' => get_class($this->cause),
258 'message' => $this->cause->getMessage(),
259 'file' => $this->cause->getFile(),
260 'line' => $this->cause->getLine());
261 } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
262 $causes[] = array('class' => get_class($this->cause),
263 'message' => $this->cause->getMessage(),
265 'line' => 'unknown');
266 } elseif (is_array($this->cause)) {
267 foreach ($this->cause as $cause) {
268 if ($cause instanceof PEAR_Exception) {
269 $cause->getCauseMessage($causes);
270 } elseif ($cause instanceof Exception) {
271 $causes[] = array('class' => get_class($cause),
272 'message' => $cause->getMessage(),
273 'file' => $cause->getFile(),
274 'line' => $cause->getLine());
275 } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
276 $causes[] = array('class' => get_class($cause),
277 'message' => $cause->getMessage(),
279 'line' => 'unknown');
280 } elseif (is_array($cause) && isset($cause['message'])) {
281 // PEAR_ErrorStack warning
283 'class' => $cause['package'],
284 'message' => $cause['message'],
285 'file' => isset($cause['context']['file']) ?
286 $cause['context']['file'] :
288 'line' => isset($cause['context']['line']) ?
289 $cause['context']['line'] :
297 public function getTraceSafe()
299 if (!isset($this->_trace)) {
300 $this->_trace = $this->getTrace();
301 if (empty($this->_trace)) {
302 $backtrace = debug_backtrace();
303 $this->_trace = array($backtrace[count($backtrace)-1]);
306 return $this->_trace;
309 public function getErrorClass()
311 $trace = $this->getTraceSafe();
312 return $trace[0]['class'];
315 public function getErrorMethod()
317 $trace = $this->getTraceSafe();
318 return $trace[0]['function'];
321 public function __toString()
323 if (isset($_SERVER['REQUEST_URI'])) {
324 return $this->toHtml();
326 return $this->toText();
329 public function toHtml()
331 $trace = $this->getTraceSafe();
333 $this->getCauseMessage($causes);
334 $html = '<table border="1" cellspacing="0">' . "\n";
335 foreach ($causes as $i => $cause) {
336 $html .= '<tr><td colspan="3" bgcolor="#ff9999">'
337 . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
338 . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
339 . 'on line <b>' . $cause['line'] . '</b>'
342 $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
343 . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
344 . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
345 . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
347 foreach ($trace as $k => $v) {
348 $html .= '<tr><td align="center">' . $k . '</td>'
350 if (!empty($v['class'])) {
351 $html .= $v['class'] . $v['type'];
353 $html .= $v['function'];
355 if (!empty($v['args'])) {
356 foreach ($v['args'] as $arg) {
357 if (is_null($arg)) $args[] = 'null';
358 elseif (is_array($arg)) $args[] = 'Array';
359 elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
360 elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
361 elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
364 $str = htmlspecialchars(substr($arg, 0, 16));
365 if (strlen($arg) > 16) $str .= '…';
366 $args[] = "'" . $str . "'";
370 $html .= '(' . implode(', ',$args) . ')'
372 . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
373 . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
374 . '</td></tr>' . "\n";
376 $html .= '<tr><td align="center">' . ($k+1) . '</td>'
378 . '<td> </td></tr>' . "\n"
383 public function toText()
386 $this->getCauseMessage($causes);
388 foreach ($causes as $i => $cause) {
389 $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
390 . $cause['message'] . ' in ' . $cause['file']
391 . ' on line ' . $cause['line'] . "\n";
393 return $causeMsg . $this->getTraceAsString();