Modified set_handler() to return dbus error code

set_handler() will call the registered callback
if set command is given to a dbus property. The
callback can return error code but the existing
code were not handling it properly.

The following patch replaced sdbusplus exception
with a negative error code in dbus-senors

https://gerrit.openbmc-project.xyz/c/openbmc/dbus-sensors/+/46444

The error code must be used to set the dbus error.
This was handled properly in case of an exception
but not if the callback was returning error code

Signed-off-by: Gokul Sanker V.G <gokul.sanker.v.g@intel.com>
Change-Id: Id164a9b3e62e65ec607d27d2219a3847d2cc7aab
diff --git a/include/sdbusplus/asio/object_server.hpp b/include/sdbusplus/asio/object_server.hpp
index 6ca8950..68e75f0 100644
--- a/include/sdbusplus/asio/object_server.hpp
+++ b/include/sdbusplus/asio/object_server.hpp
@@ -20,6 +20,7 @@
 #include <list>
 #include <optional>
 #include <set>
+#include <tuple>
 
 namespace sdbusplus
 {
@@ -47,7 +48,7 @@
 {
   public:
     virtual ~callback_set() = default;
-    virtual SetPropertyReturnValue call(message_t& m) = 0;
+    virtual std::tuple<SetPropertyReturnValue, int> call(message_t& m) = 0;
     virtual SetPropertyReturnValue set(const boost::any& value) = 0;
 };
 
@@ -311,21 +312,23 @@
         value_(value),
         func_(std::move(func))
     {}
-    SetPropertyReturnValue call(message_t& m) override
+    std::tuple<SetPropertyReturnValue, int> call(message_t& m) override
     {
         PropertyType input;
         m.read(input);
 
         auto oldValue = *value_;
-        if (func_(input, *value_))
+        auto ret = func_(input, *value_);
+
+        if (ret < 0)
         {
-            if (oldValue == *value_)
-            {
-                return SetPropertyReturnValue::sameValueUpdated;
-            }
-            return SetPropertyReturnValue::valueUpdated;
+            return {SetPropertyReturnValue::fail, ret};
         }
-        return SetPropertyReturnValue::fail;
+        if (oldValue == *value_)
+        {
+            return {SetPropertyReturnValue::sameValueUpdated, ret};
+        }
+        return {SetPropertyReturnValue::valueUpdated, ret};
     }
     SetPropertyReturnValue set(const boost::any& value) override
     {
@@ -664,17 +667,26 @@
             try
             {
 #endif
-                SetPropertyReturnValue status = func->second->call(mesg);
-                if ((status == SetPropertyReturnValue::valueUpdated) ||
-                    (status == SetPropertyReturnValue::sameValueUpdated))
+                auto [status, rc] = func->second->call(mesg);
+
+                switch (status)
                 {
-                    if (status != SetPropertyReturnValue::sameValueUpdated)
+                    case SetPropertyReturnValue::sameValueUpdated:
+                    {
+                        return true;
+                    }
+                    case SetPropertyReturnValue::valueUpdated:
                     {
                         data->signal_property(property);
+                        return true;
                     }
-                    return true;
+                    case SetPropertyReturnValue::fail:
+                    {
+                        sd_bus_error_set_errno(error, rc);
+                        return false;
+                    }
                 }
-                return false;
+
 #ifdef __EXCEPTIONS
             }