Tracing PEAR Errors

From: Date: Sun, 07 Sep 2008 14:38:27 +0000
Subject: Tracing PEAR Errors
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-29239@lists.php.net to get a copy of this message
Greetings, All. Don't worry about reduced stacktrace. If you actually catching exception, you can retrieve whole trace using native getTraceAsString() method. Cutting was introduces due to limited length of exception string (about 1015 characters max). <?php /** Base classes * * @package * @subpackage * @author Daemon * @version $Id$ * @copyright 2008 */ require_once('PEAR.php'); class ePearError extends Exception { protected $_type; protected $_userInfo; function getType() { return $this->_type; } function getDebugInfo() { return $this->getUserInfo(); } function getUserInfo() { return $this->_userInfo; } function __construct(&$errorObject) { parent::__construct($errorObject->getMessage(), $errorObject->getCode()); $this->_type = $errorObject->getType(); $this->_userInfo = $errorObject->getUserInfo(); } protected function getTraceAsString2() { $trace = array_reverse($this->getTrace(), true); foreach($trace as $n => $info) { if(array_key_exists('file', $info)) { $_file = $info['file'] . ':' . $info['line']; if(30 < strlen($_file)) { $_file = '..' . substr($_file, -28); } } else { $_file = '[internal function]'; } $_class = array_key_exists('class', $info) ? $info['class'] . $info['type'] : ''; if(array_key_exists('function', $info)) { // Drop error calling sequence to reduce backtrace length if(in_array($info['function'], array('PEAR_Error', 'raiseError'))) { break; } $_args = ''; foreach($info['args'] as $a) { $_args .= empty($_args) ? '' : ', '; switch($t = (string)gettype($a)) { /* Possibles values for the returned string are: "boolean" (since PHP 4) "integer" "double" (for historical reasons "double" is returned in case of a float, and not simply "float") "string" "array" "object" "resource" (since PHP 4) "NULL" (since PHP 4) "user function" (PHP 3 only, deprecated) "unknown type" */ case 'boolean': $_args .= $a ? 'true' : 'false'; break; case 'integer': case 'double': case 'resource': $_args .= strval($a); break; case 'string': if(10 < strlen($a)) { $a = substr($a, 0, 7) . '..'; } $_args .= '\'' . preg_replace('/[\x00-\x1F\x7F]/', '?', $a) . '\''; break; case 'array': $_args .= 'Array'; break; case 'object': $a = get_class($a); $_args .= "Object({$a})"; break; case 'NULL': $_args .= 'NULL'; break; default: $_args .= "?(unknown)"; } } $_func = $info['function'] . "({$_args})"; } else { $_func = ''; } $result .= "#{$n} {$_file} {$_class}{$_func}\n";; } return $result; } function __toString() { $result = sprintf('%s with message "%s"', $this->getType(), $this->getMessage()); $result .= "\nStack trace:\n" . $this->getTraceAsString2(); return $result; } } PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, create_function('$e', 'throw new ePearError($e);')); ?> -- Sincerely Yours, ANR Daemon <anrdaemon@freemail.ru>

« previous php.pear.general (#29239) next »