From 27b676f91e843ca6070e5dcd159c6506e73d72a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 17 Jun 2021 16:02:24 +0200
Subject: [PATCH] :ambulance: fix(exceptions) Improve exception messages

In some cases they end up shown to the user and should be
 understandable.

issue #6170
---
 include/class_exceptions.inc                | 78 +++++++++++++++++++++
 include/class_objects.inc                   |  6 +-
 include/simpleplugin/class_simplePlugin.inc |  2 +-
 plugins/personal/generic/class_user.inc     |  2 +-
 4 files changed, 83 insertions(+), 5 deletions(-)

diff --git a/include/class_exceptions.inc b/include/class_exceptions.inc
index 10fa9fc62..3ea0cea11 100644
--- a/include/class_exceptions.inc
+++ b/include/class_exceptions.inc
@@ -69,18 +69,79 @@ class InvalidValueException extends FusionDirectoryException
 
 class NonExistingObjectTypeException extends FusionDirectoryException
 {
+  protected $type;
+
+  public function __construct (string $type, int $code = 0, Throwable $previous = NULL)
+  {
+    $this->type = $type;
+
+    parent::__construct(sprintf(_('Non-existing type "%s"!'), $this->type), $code, $previous);
+  }
+
+  public function toArray (): array
+  {
+    $array = parent::toArray();
+
+    $array['type'] = $this->type;
+
+    return $array;
+  }
 }
 
 class NonExistingBranchException extends FusionDirectoryException
 {
+  protected $branch;
+
+  public function __construct (string $branch, int $code = 0, Throwable $previous = NULL)
+  {
+    $this->branch = $branch;
+
+    parent::__construct(sprintf(_('Non-existing branch "%s"!'), $this->branch), $code, $previous);
+  }
+
+  public function toArray (): array
+  {
+    $array = parent::toArray();
+
+    $array['branch'] = $this->branch;
+
+    return $array;
+  }
 }
 
 class NonExistingLdapNodeException extends FusionDirectoryException
 {
+  protected $dn;
+
+  public function __construct (string $dn, string $message = NULL, int $code = 0, Throwable $previous = NULL)
+  {
+    $this->dn = $dn;
+    if ($message === NULL) {
+      $message = sprintf(_('Non-existing dn "%s"!'), $this->dn);
+    }
+
+    parent::__construct($message, $code, $previous);
+  }
+
+  public function toArray (): array
+  {
+    $array = parent::toArray();
+
+    $array['dn'] = $this->dn;
+
+    return $array;
+  }
 }
 
 class EmptyFilterException extends FusionDirectoryException
 {
+  public function __construct (string $message = "", int $code = 0, Throwable $previous = NULL)
+  {
+    if ($message === '') {
+      $message = _('Filter is empty');
+    }
+    parent::__construct($message, $code, $previous);
+  }
 }
 
 class NoManagementClassException extends FusionDirectoryException
@@ -89,6 +150,23 @@ class NoManagementClassException extends FusionDirectoryException
 
 class UnknownClassException extends FusionDirectoryException
 {
+  protected $class;
+
+  public function __construct (string $class, int $code = 0, Throwable $previous = NULL)
+  {
+    $this->class = $class;
+
+    parent::__construct(sprintf(_('Unknown class "%s"!'), $this->class), $code, $previous);
+  }
+
+  public function toArray (): array
+  {
+    $array = parent::toArray();
+
+    $array['unknownclass'] = $this->class;
+
+    return $array;
+  }
 }
 
 class LDAPFailureException extends FusionDirectoryException
diff --git a/include/class_objects.inc b/include/class_objects.inc
index fc82e2627..44f6798bc 100644
--- a/include/class_objects.inc
+++ b/include/class_objects.inc
@@ -273,7 +273,7 @@ class objects
 
     $ldap = $config->get_ldap_link($sizeLimit);
     if (!$ldap->dn_exists($ou)) {
-      throw new NonExistingBranchException();
+      throw new NonExistingBranchException($ou);
     }
     if (empty($filter)) {
       $filter = '(|'.implode($typeFilters).')';
@@ -379,7 +379,7 @@ class objects
           $text = $dn;
         }
       } else {
-        throw new NonExistingLdapNodeException('Dn '.$dn.' not found in LDAP');
+        throw new NonExistingLdapNodeException($dn);
       }
     } elseif (is_array($text)) {
       $text = $text[$infos['nameAttr']][0];
@@ -430,7 +430,7 @@ class objects
     global $config;
 
     if (!isset($config->data['OBJECTS'][strtoupper($type)])) {
-      throw new NonExistingObjectTypeException('Non-existing type "'.$type.'"');
+      throw new NonExistingObjectTypeException($type);
     }
 
     $infos =& $config->data['OBJECTS'][strtoupper($type)];
diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc
index 7dff3485e..3efe722f6 100644
--- a/include/simpleplugin/class_simplePlugin.inc
+++ b/include/simpleplugin/class_simplePlugin.inc
@@ -234,7 +234,7 @@ class simplePlugin implements SimpleTab
         $ldap->cat($this->dn);
         $this->attrs = $ldap->fetch(TRUE);
         if (empty($this->attrs)) {
-          throw new NonExistingLdapNodeException('Could not open dn '.$this->dn);
+          throw new NonExistingLdapNodeException($this->dn);
         }
         if ($this->mainTab) {
           $this->entryCSN = getEntryCSN($this->dn);
diff --git a/plugins/personal/generic/class_user.inc b/plugins/personal/generic/class_user.inc
index 4bd5ef1f7..0cd564ba0 100644
--- a/plugins/personal/generic/class_user.inc
+++ b/plugins/personal/generic/class_user.inc
@@ -427,7 +427,7 @@ class user extends simplePlugin
       $ldap->cat($ppolicydn, ['pwdAllowUserChange', 'pwdMinLength', 'pwdMinAge', 'pwdSafeModify', 'pwdExpireWarning', 'pwdMaxAge']);
       $policy = $ldap->fetch(TRUE);
       if (!$policy) {
-        throw new NonExistingLdapNodeException(sprintf(_('Ppolicy "%s" could not be found in the LDAP!'), $ppolicydn));
+        throw new NonExistingLdapNodeException($ppolicydn, sprintf(_('Ppolicy "%s" could not be found in the LDAP!'), $ppolicydn));
       }
     }
 
-- 
GitLab