From cc76507b84385e953fb202298253190a52638ab9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 13 Aug 2020 11:19:22 +0200
Subject: [PATCH] :ambulance: fix(errors) Improve string conversion of errors

When errors get converted to strings in CSV import error dialog, it
 outputs the trace which is way too much text. Errors should convert to
 their error message, to look the same as before we used error objects.

issue #6071
---
 include/errors/class_FusionDirectoryError.inc |  5 ++++
 include/errors/class_SimplePluginError.inc    | 24 +++++++++++++++++++
 include/simpleplugin/interface_SimpleTab.inc  |  2 ++
 3 files changed, 31 insertions(+)

diff --git a/include/errors/class_FusionDirectoryError.inc b/include/errors/class_FusionDirectoryError.inc
index 661e67b1d..418c3164b 100644
--- a/include/errors/class_FusionDirectoryError.inc
+++ b/include/errors/class_FusionDirectoryError.inc
@@ -45,6 +45,11 @@ class FusionDirectoryError extends Error
     ];
   }
 
+  public function __toString ()
+  {
+    return $this->getMessage();
+  }
+
   public function computeMsgDialogParameters (): array
   {
     return [_('Error'), $this->htmlMessage, ERROR_DIALOG, static::formatTrace($this)];
diff --git a/include/errors/class_SimplePluginError.inc b/include/errors/class_SimplePluginError.inc
index 397077e3f..22b2f2cec 100644
--- a/include/errors/class_SimplePluginError.inc
+++ b/include/errors/class_SimplePluginError.inc
@@ -76,6 +76,30 @@ class SimplePluginError extends FusionDirectoryError
     return $array;
   }
 
+  public function __toString ()
+  {
+    $msg = '';
+
+    if (isset($this->object)) {
+      $msg .= $this->object->getBaseObject()->dn.' > ';
+    }
+
+    if (isset($this->tab) && isset($this->tab->parent->by_name[get_class($this->tab)])) {
+      $msg .= $this->tab->parent->by_name[get_class($this->tab)].' > ';
+    }
+
+    if (isset($this->attribute)) {
+      $label = $this->attribute->getLabel();
+      if (empty($label)) {
+        $msg .= $this->attribute->getLdapName();
+      } else {
+        $msg .= $label;
+      }
+    }
+
+    return $msg.': '.$this->getMessage();
+  }
+
   public function computeMsgDialogParameters (): array
   {
     $html = '';
diff --git a/include/simpleplugin/interface_SimpleTab.inc b/include/simpleplugin/interface_SimpleTab.inc
index aab62ef21..d8428e6d8 100644
--- a/include/simpleplugin/interface_SimpleTab.inc
+++ b/include/simpleplugin/interface_SimpleTab.inc
@@ -135,6 +135,8 @@ interface SimpleTab
 
   /*!
    * \brief Deserialize values
+   *
+   * Returns TRUE or error
    */
   public function deserializeValues (array $values, bool $checkAcl = TRUE);
 
-- 
GitLab