Handle InvalidArgument error for hostname

Systemd throws InvalidArgument error for hostname with special
characters.

This commit handles InvalidArgument error.

Tested By:
verified by passing hostname with special char

Change-Id: I732ce9705044d45a25c05f0164bfa5e47e03f025
Signed-off-by: Ravi Teja <raviteja28031990@gmail.com>
diff --git a/src/system_configuration.cpp b/src/system_configuration.cpp
index 6eb1588..b008dba 100644
--- a/src/system_configuration.cpp
+++ b/src/system_configuration.cpp
@@ -14,7 +14,9 @@
 static constexpr char HOSTNAMED_OBJ[] = "/org/freedesktop/hostname1";
 static constexpr char HOSTNAMED_INTF[] = "org.freedesktop.hostname1";
 
+using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+using Argument = xyz::openbmc_project::Common::InvalidArgument;
 
 static constexpr char propMatch[] =
     "type='signal',sender='org.freedesktop.hostname1',"
@@ -77,12 +79,21 @@
         auto method = bus.get().new_method_call(
             HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
         method.append(name, /*interactive=*/false);
-        bus.get().call_noreply(method);
+        method.call();
         return SystemConfigIntf::hostName(std::move(name));
     }
-    catch (const std::exception& e)
+    catch (const sdbusplus::exception::SdBusError& e)
     {
-        lg2::error("Failed to set hostname: {ERROR}", "ERROR", e);
+        lg2::error("Failed to set hostname {HOSTNAME}: {ERROR} ", "HOSTNAME",
+                   name, "ERROR", e);
+        auto dbusError = e.get_error();
+        if ((dbusError != nullptr) &&
+            (strcmp(dbusError->name,
+                    "org.freedesktop.DBus.Error.InvalidArgs") == 0))
+        {
+            elog<InvalidArgument>(Argument::ARGUMENT_NAME("Hostname"),
+                                  Argument::ARGUMENT_VALUE(name.c_str()));
+        }
     }
     return SystemConfigIntf::hostName();
 }