cvs: pear-core /PEAR ErrorStack5.php Exception.php

From: Date: Thu, 02 Sep 2004 02:57:56 +0000
Subject: cvs: pear-core /PEAR ErrorStack5.php Exception.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-cvs+get-23571@lists.php.net to get a copy of this message
cellog Wed Sep 1 22:57:56 2004 EDT Modified files: /pear-core/PEAR ErrorStack5.php Exception.php Log: add full exception support into PEAR_ErrorStack (note - are these 1-liners really necessary?) add support for both multiple exceptions causing an exception and multiple warnings in PEAR_Exception http://cvs.php.net/diff.php/pear-core/PEAR/ErrorStack5.php?r1=1.1&r2=1.2&ty=u Index: pear-core/PEAR/ErrorStack5.php diff -u pear-core/PEAR/ErrorStack5.php:1.1 pear-core/PEAR/ErrorStack5.php:1.2 --- pear-core/PEAR/ErrorStack5.php:1.1 Sat Jul 10 14:20:42 2004 +++ pear-core/PEAR/ErrorStack5.php Wed Sep 1 22:57:56 2004 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: ErrorStack5.php,v 1.1 2004/07/10 18:20:42 cellog Exp $ +// $Id: ErrorStack5.php,v 1.2 2004/09/02 02:57:56 cellog Exp $ /** * Error Stack Implementation - E_STRICT compliant @@ -427,7 +427,31 @@ self::$overridecallback = array(); } } - + + /** + * demote an exception to a non-fatal error (default is warning) + * @param Exception + * @param string + */ + public function demoteException($e, $level = 'warning') + { + $this->push(get_class($e), $level, array(), + $e->getMessage(), $e, $e->getTrace()); + } + + /** + * promote a warning into a PEAR_Exception + * + * It is probably best to do this manually, to take advantage of the + * use of exception classnames to categorize errors + * @return PEAR_Exception + */ + public function promoteWarning($warning, $exceptionclass = 'PEAR_Exception') + { + return new PEAR_Exception($warning['message'], + array($warning), $warning['code']); + } + /** * Add an error to the stack * http://cvs.php.net/diff.php/pear-core/PEAR/Exception.php?r1=1.10&r2=1.11&ty=u Index: pear-core/PEAR/Exception.php diff -u pear-core/PEAR/Exception.php:1.10 pear-core/PEAR/Exception.php:1.11 --- pear-core/PEAR/Exception.php:1.10 Wed Sep 1 22:36:00 2004 +++ pear-core/PEAR/Exception.php Wed Sep 1 22:57:56 2004 @@ -18,7 +18,7 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: Exception.php,v 1.10 2004/09/02 02:36:00 cellog Exp $ +// $Id: Exception.php,v 1.11 2004/09/02 02:57:56 cellog Exp $ define('PEAR_OBSERVER_PRINT', -2); define('PEAR_OBSERVER_TRIGGER', -4); @@ -32,7 +32,9 @@ * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception)) * - Definable triggers, shot when exceptions occur * - Pretty and informative error messages - * - Added more context info avaible (like class, method or cause) + * - Added more context info available (like class, method or cause) + * - cause can be a PEAR_Exception or an array of mixed + * PEAR_Exceptions/PEAR_ErrorStack warnings * * 2) Ideas: * @@ -85,7 +87,7 @@ * * @since PHP 5 * @package PEAR - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ * @author Tomas V.V.Cox <cox@idecnet.com> * @author Hans Lellelid <hans@velum.net> * @author Bertrand Mansion <bmansion@mamasam.com> @@ -103,13 +105,15 @@ * PEAR_Exception(string $message, int $code); * PEAR_Exception(string $message, Exception $cause); * PEAR_Exception(string $message, Exception $cause, int $code); + * PEAR_Exception(string $message, array $causes); + * PEAR_Exception(string $message, array $causes, int $code); */ public function __construct($message, $p2 = null, $p3 = null) { if (is_int($p2)) { $code = $p2; $this->cause = null; - } elseif ($p2 instanceof Exception) { + } elseif ($p2 instanceof Exception || is_array($p2)) { $code = $p3; $this->cause = $p2; } else { @@ -168,14 +172,18 @@ /** * Returns the exception that caused this exception to be thrown * @access public - * @return Exception_object The context of the exception + * @return Exception|array The context of the exception */ public function getCause() { return $this->cause; } - private function _getCauseMessage(&$causes) + /** + * Function must be public to call on caused exceptions + * @param array + */ + public function getCauseMessage(&$causes) { $trace = $this->getTraceSafe(); $causes[] = array('class' => get_class($this), @@ -183,7 +191,26 @@ 'file' => $trace[0]['file'], 'line' => $trace[0]['line']); if ($this->cause instanceof PEAR_Exception) { - $this->cause->_getCauseMessage($causes); + $this->cause->getCauseMessage($causes); + } + if (is_array($this->cause)) { + foreach ($this->cause as $cause) { + if ($cause instanceof PEAR_Exception) { + $cause->getCauseMessage($causes); + } elseif (is_array($cause) && isset($cause['message'])) { + // PEAR_ErrorStack warning + $causes[] = array( + 'class' => $cause['package'], + 'message' => $cause['message'], + 'file' => isset($cause['context']['file']) ? + $cause['context']['file'] : + 'unknown', + 'line' => isset($cause['context']['line']) ? + $cause['context']['line'] : + 'unknown', + ); + } + } } } @@ -223,7 +250,7 @@ { $trace = $this->getTraceSafe(); $causes = array(); - $this->_getCauseMessage($causes); + $this->getCauseMessage($causes); $html = '<table border="1" cellspacing="0">' . "\n"; foreach ($causes as $i => $cause) { $html .= '<tr><td colspan="3" bgcolor="#ff9999">' @@ -274,7 +301,7 @@ public function toText() { $causes = array(); - $this->_getCauseMessage($causes); + $this->getCauseMessage($causes); $causeMsg = ''; foreach ($causes as $i => $cause) { $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '

« previous php.pear.cvs (#23571) next »