Add PEAR Error handler that converts the error into an exception and adds the PEAR...
authorGunnar Wrobel <p@rdus.de>
Tue, 26 Oct 2010 08:11:12 +0000 (10:11 +0200)
committerGunnar Wrobel <p@rdus.de>
Tue, 26 Oct 2010 08:11:12 +0000 (10:11 +0200)
framework/Exception/lib/Horde/Exception/Pear.php [new file with mode: 0644]
framework/Exception/package.xml

diff --git a/framework/Exception/lib/Horde/Exception/Pear.php b/framework/Exception/lib/Horde/Exception/Pear.php
new file mode 100644 (file)
index 0000000..e0d43b7
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * Horde exception class that converts PEAR errors to exceptions.
+ *
+ * Copyright 2008-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Horde_Exception
+ */
+class Horde_Exception_Pear extends Horde_Exception
+{
+    /**
+     * The class name for generated exceptions.
+     *
+     * @var string
+     */
+    static protected $_class = __CLASS__;
+
+    /**
+     * The original PEAR error.
+     *
+     * @var PEAR_Error
+     */
+    private $_error;
+
+    /**
+     * Exception constructor.
+     *
+     * @param PEAR_Error $error The PEAR error.
+     */
+    public function __construct(PEAR_Error $error)
+    {
+        parent::__construct(
+            $error->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;
+    }
+}
index 9cab2c3..a73e321 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<package packagerversion="1.9.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+<package packagerversion="1.9.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
  <name>Exception</name>
  <channel>pear.horde.org</channel>
  <summary>Horde Exception Handler</summary>
@@ -22,8 +22,8 @@
   <email>slusarz@horde.org</email>
   <active>yes</active>
  </developer>
- <date>2010-10-22</date>
- <time>19:03:02</time>
+ <date>2010-10-26</date>
+ <time>08:24:18</time>
  <version>
   <release>0.1.0</release>
   <api>0.1.0</api>
@@ -43,6 +43,7 @@
      <dir name="Exception">
       <file name="LastError.php" role="php" />
       <file name="NotFound.php" role="php" />
+      <file name="Pear.php" role="php" />
       <file name="PermissionDenied.php" role="php" />
       <file name="Prior.php" role="php" />
       <file name="Translation.php" role="php">
    <install as="Horde/Exception.php" name="lib/Horde/Exception.php" />
    <install as="Horde/Exception/LastError.php" name="lib/Horde/Exception/LastError.php" />
    <install as="Horde/Exception/NotFound.php" name="lib/Horde/Exception/NotFound.php" />
+   <install as="Horde/Exception/Pear.php" name="lib/Horde/Exception/Pear.php" />
    <install as="Horde/Exception/PermissionDenied.php" name="lib/Horde/Exception/PermissionDenied.php" />
    <install as="Horde/Exception/Prior.php" name="lib/Horde/Exception/Prior.php" />
    <install as="Horde/Exception/Translation.php" name="lib/Horde/Exception/Translation.php" />
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2010-10-22</date>
+   <date>2010-10-26</date>
    <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
    <notes>
 * Initial package.