From 36e5a47332c4db59c861975880ebf1de00f5ddf8 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 26 Oct 2010 10:11:12 +0200 Subject: [PATCH] Add PEAR Error handler that converts the error into an exception and adds the PEAR Error stack trace to the exception message. --- framework/Exception/lib/Horde/Exception/Pear.php | 81 ++++++++++++++++++++++++ framework/Exception/package.xml | 10 +-- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 framework/Exception/lib/Horde/Exception/Pear.php diff --git a/framework/Exception/lib/Horde/Exception/Pear.php b/framework/Exception/lib/Horde/Exception/Pear.php new file mode 100644 index 000000000..e0d43b771 --- /dev/null +++ b/framework/Exception/lib/Horde/Exception/Pear.php @@ -0,0 +1,81 @@ +getMessage() . $this->_getPearTrace(), + $error->getCode() + ); + } + + /** + * Return a trace for the PEAR error. + * + * @param PEAR_Error $error The PEAR error. + * + * @return string The backtrace as a string. + */ + private function _getPearTrace(PEAR_Error $error) + { + $backtrace = $error->getBacktrace(); + if (!empty($backtrace)) { + $pear_error .= "\n\n" . 'PEAR Error:' . "\n"; + foreach ($backtrace as $frame) { + $pear_error .= ' ' . $frame['class'] . '->' + . $frame['function'] . ' ' . $frame['file'] + . ':' . $frame['line'] . "\n"; + } + $pear_error .= "\n"; + return $pear_error; + } + return ''; + } + + /** + * Exception handling. + * + * @param mixed $result The result to be checked for a PEAR_Error. + * + * @return mixed Returns the original result if it was no PEAR_Error. + * + * @throws Horde_Exception_Pear In case the result was a PEAR_Error. + */ + static public function catchError($result) + { + if ($result instanceOf PEAR_Error) { + throw new self::$_class($result); + } + return $result; + } +} diff --git a/framework/Exception/package.xml b/framework/Exception/package.xml index 9cab2c343..a73e321e4 100644 --- a/framework/Exception/package.xml +++ b/framework/Exception/package.xml @@ -1,5 +1,5 @@ - + Exception pear.horde.org Horde Exception Handler @@ -22,8 +22,8 @@ slusarz@horde.org yes - 2010-10-22 - + 2010-10-26 + 0.1.0 0.1.0 @@ -43,6 +43,7 @@ + @@ -352,6 +353,7 @@ + @@ -458,7 +460,7 @@ beta beta - 2010-10-22 + 2010-10-26 LGPL * Initial package. -- 2.11.0