From d7132a1fa49bf1f9b3e9615aad6efe236b83f5cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org> Date: Thu, 17 Dec 2020 12:33:04 +0100 Subject: [PATCH] :sparkles: feat(core) Use error objects for deserializing errors This will improve webservice error context. issue #6071 --- include/class_exceptions.inc | 9 +++++++++ include/errors/class_FusionDirectoryError.inc | 1 + .../attributes/class_BooleanAttribute.inc | 10 ++++++++-- .../attributes/class_FileAttribute.inc | 17 ++++++++++------ include/simpleplugin/class_Attribute.inc | 5 ++++- include/simpleplugin/class_simplePlugin.inc | 7 +++++-- .../generic/class_UserPasswordAttribute.inc | 20 +++++++++++++++---- 7 files changed, 54 insertions(+), 15 deletions(-) diff --git a/include/class_exceptions.inc b/include/class_exceptions.inc index e127526cd..10fa9fc62 100644 --- a/include/class_exceptions.inc +++ b/include/class_exceptions.inc @@ -28,6 +28,15 @@ */ class FusionDirectoryException extends Exception { + public function toArray (): array + { + return [ + 'class' => get_class($this), + 'message' => $this->getMessage(), + 'line' => $this->getLine(), + 'file' => $this->getFile(), + ]; + } } /*! \class LDIFImportException diff --git a/include/errors/class_FusionDirectoryError.inc b/include/errors/class_FusionDirectoryError.inc index 418c3164b..6a694270d 100644 --- a/include/errors/class_FusionDirectoryError.inc +++ b/include/errors/class_FusionDirectoryError.inc @@ -39,6 +39,7 @@ class FusionDirectoryError extends Error public function toArray (): array { return [ + 'class' => get_class($this), 'message' => $this->getMessage(), 'line' => $this->getLine(), 'file' => $this->getFile(), diff --git a/include/simpleplugin/attributes/class_BooleanAttribute.inc b/include/simpleplugin/attributes/class_BooleanAttribute.inc index 52ab02ac7..a89c136ae 100644 --- a/include/simpleplugin/attributes/class_BooleanAttribute.inc +++ b/include/simpleplugin/attributes/class_BooleanAttribute.inc @@ -166,7 +166,10 @@ class BooleanAttribute extends Attribute function deserializeValue ($value) { if ($this->disabled) { - return sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName())) + ); } if ($value === $this->trueValue) { $this->setValue(TRUE); @@ -179,7 +182,10 @@ class BooleanAttribute extends Attribute } elseif ($value === 0) { $this->setValue(FALSE); } else { - return sprintf(_('"%s" is not a valid value for attribute "%s" should be "%s" or "%s"'), $value, $this->getLdapName(), $this->trueValue, $this->falseValue); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('"%s" is not a valid value, should be "%s" or "%s"'), $value, $this->getLdapName(), $this->trueValue, $this->falseValue)) + ); } /* No error */ diff --git a/include/simpleplugin/attributes/class_FileAttribute.inc b/include/simpleplugin/attributes/class_FileAttribute.inc index 098e39887..05abb5e68 100644 --- a/include/simpleplugin/attributes/class_FileAttribute.inc +++ b/include/simpleplugin/attributes/class_FileAttribute.inc @@ -134,12 +134,18 @@ class FileAttribute extends Attribute function deserializeValue ($value) { if ($this->disabled) { - return sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName())) + ); } if ($this->binary) { $data = base64_decode($value, TRUE); if ($data === FALSE) { - return sprintf(_('Invalid base64 data for attribute %s'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(_('Invalid base64 data')) + ); } $this->setValue($data); } else { @@ -394,14 +400,13 @@ class ImageAttribute extends FileAttribute } } catch (ImagickException $e) { /* Store the exception to return it in deserializeValue() */ - $this->imagickException = $e; - $error = new SimplePluginError( + $this->imagickException = new SimplePluginError( $this, SimplePluginCheckError::invalidValue($e->getMessage()), 0, $e ); - $error->display(); + $this->imagickException->display(); } } else { $error = new SimplePluginError( @@ -423,7 +428,7 @@ class ImageAttribute extends FileAttribute return $error; } if ($this->imagickException !== NULL) { - return sprintf(_('Invalid data, Imagick error: "%s"'), $this->imagickException->getMessage()); + return $this->imagickException; } } diff --git a/include/simpleplugin/class_Attribute.inc b/include/simpleplugin/class_Attribute.inc index c7d4d8729..2d298f490 100644 --- a/include/simpleplugin/class_Attribute.inc +++ b/include/simpleplugin/class_Attribute.inc @@ -695,7 +695,10 @@ class Attribute function deserializeValue ($value) { if ($this->disabled) { - return sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName())) + ); } $this->setValue($value); } diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc index 55dc16356..7dff3485e 100644 --- a/include/simpleplugin/class_simplePlugin.inc +++ b/include/simpleplugin/class_simplePlugin.inc @@ -2059,10 +2059,13 @@ class simplePlugin implements SimpleTab return $error; } } else { - return msgPool::permModify($this->dn, $name); + return new SimplePluginPermissionError($this, msgPool::permModify($this->dn, $name)); } } else { - return sprintf(_('Unknown field "%s"'), $name); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Unknown field "%s"'), $name)) + ); } } return TRUE; diff --git a/plugins/personal/generic/class_UserPasswordAttribute.inc b/plugins/personal/generic/class_UserPasswordAttribute.inc index 230002399..1c7fc460c 100644 --- a/plugins/personal/generic/class_UserPasswordAttribute.inc +++ b/plugins/personal/generic/class_UserPasswordAttribute.inc @@ -273,17 +273,29 @@ class UserPasswordAttribute extends CompositeAttribute } if (is_array($value)) { if (count($value) > 5) { - return sprintf(_('Too many elements in array value for password field %s: %d instead of %d'), $this->getLdapName(), count($value), 5); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Too many elements in array value: %d instead of %d'), $this->getLdapName(), count($value), 5)) + ); } elseif (count($value) < 5) { - return sprintf(_('Not enough elements in array value for password field %s: %d instead of %d'), $this->getLdapName(), count($value), 5); + return new SimplePluginError( + $this, + htmlescape(sprintf(_('Not enough elements in array value: %d instead of %d'), $this->getLdapName(), count($value), 5)) + ); } elseif (!isset($value[0])) { - return sprintf(_('Array value for password field %s must have numeric keys'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(_('Array value for password field must have numeric keys')) + ); } $this->setValue($value); } elseif (is_string($value)) { $this->setValue(['', $value, $value, '', FALSE]); } else { - return sprintf(_('Invalid value type for password field %s, must be array or string'), $this->getLdapName()); + return new SimplePluginError( + $this, + htmlescape(_('Invalid value type for password field, must be array or string')) + ); } } } -- GitLab