[turba] Re: How to return a PEAR_Error in PHP? - fixed

John Dalbec jpdalbec at ysu.edu
Thu Nov 6 10:08:20 PST 2003



Chuck Hagenbuch wrote:

> Quoting John Dalbec <jpdalbec at ysu.edu>:
> 
> 
>>I'm trying to convert the LDAP driver search() function in Turba to return a
>>PEAR_Error if there is a problem with the search.  No matter what I do the
>>PEAR::isError function returns false on the returned function value.  What am
>>I missing?  The php.net documentation is giving me a headache and I'm not
>>finding an answer.  I thought maybe I needed to pass a reference instead of a
>>copy, but that's not helping.  My environment is Red Hat Linux 7.3, PHP
>>4.1.2-7.3.6 RPM. Is this a PHP bug in that version?
> 
> 
> That's an old and possibly buggy PHP version, but we really can't tell you since
> you didn't show us the code in question - we have no idea what you may or may
> not be doing wrong.

I updated lib/Driver/ldap.php, search.php, and browse.php, but I forgot 
lib/Source.php (D'oh!).  I was returning a PEAR_Error from lib/Driver/ldap.php 
but lib/Source.php had no idea what to do with it and returned an empty 
Turba_List.  Anyway, I've attached the final result if you're curious.  This is 
against Turba 1.2.1.
John

> -chuck
> 
> --
> Charles Hagenbuch, <chuck at horde.org>
> "I am not that interested in green food." - Average Joe
> 
-------------- next part --------------
--- /var/www/html/horde/turba/browse.php	Wed Sep 24 15:25:36 2003
+++ /var/www/html/horde/turba/browse.php	Wed Sep 24 15:25:36 2003
@@ -181,6 +181,13 @@
 
     if (!empty($_SESSION['turba_search_results']) &&
         Horde::getFormData('key') == '**search') {
+
+        if ($_SESSION['turba_search_errflag']) {
+            $_SESSION['turba_search_errflag'] = false;
+            Horde::raiseMessage(sprintf('%s (%d) %s', _('Error while searching directory.'), $_SESSION['turba_search_errno'], _($_SESSION['turba_search_errstr'])), HORDE_ERROR);
+            include TURBA_BASE . '/status.php';
+        }
+
         /* We are displaying some search results */
         $results = Turba_List::unserialize($_SESSION['turba_search_results']);
         $results->sort($sortcolumn, null, null, $prefs->getValue('sortdir'));
--- /var/www/html/horde/turba/search.php	Fri Apr 11 09:13:00 2003
+++ /var/www/html/horde/turba/search.php	Fri Apr 11 09:13:00 2003
@@ -47,6 +47,11 @@
         Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR);
     } else {
         if (is_object($results = $driver->search(array($criteria => $val)))) {
+            if ($_SESSION['turba_search_errflag'] = PEAR::isError($results)) {
+                $_SESSION['turba_search_errstr']  = $results->getMessage(); 
+                $_SESSION['turba_search_errno']   = $results->getCode();
+                $results = $results->getUserInfo();
+            }
             if (!is_object($_SESSION['turba_search_results'])) {
                 $_SESSION['turba_search_results'] = new Turba_List();
             }
--- /var/www/html/horde/turba/lib/Source.php	Mon Jan 27 17:08:44 2003
+++ /var/www/html/horde/turba/lib/Source.php	Mon Jan 27 17:08:44 2003
@@ -204,6 +204,12 @@
         /* Retrieve the search results from the driver. */
         $results = $this->driver->search($fields, array_values($this->map), $strict_fields, $match);
 
+        if ($search_errflag = PEAR::isError($results)) {
+            $search_errstr  = $results->getMessage();
+            $search_errno   = $results->getCode();
+            $results = $results->getUserInfo();
+        }
+
         /*
          * Translate the driver-specific fields in the result back to the
          * more generalized common Turba attributes using the map.
@@ -216,6 +222,11 @@
         }
         $list->sort($sort_criteria, null, null, $sort_direction);
 
+        if ($search_errflag) {
+            return new PEAR_Error($search_errstr, $search_errno, null, null,
+                                  $list);
+        }
+
         /* Return the filtered (sorted) results. */
         return $list;
     }
--- /var/www/html/horde/turba/lib/Driver/ldap.php	Tue Feb 25 21:40:48 2003
+++ /var/www/html/horde/turba/lib/Driver/ldap.php	Tue Feb 25 21:40:48 2003
@@ -170,7 +170,12 @@
             $this->errno = ldap_errno($this->ds);
             $this->errstr = ldap_error($this->ds);
             // FIXME: raise an exception using PEAR error handling
-            return array();
+            return new PEAR_Error($this->errstr, $this->errno, null, null,
+                                    array());
+        } elseif ($this->errno = ldap_errno($this->ds)) {
+            $this->errstr = ldap_error($this->ds);
+            return new PEAR_Error($this->errstr, $this->errno, null, null,
+                                    $this->getResults($fields, $res));
         }
 
         return $this->getResults($fields, $res);


More information about the turba mailing list