Fix error codes

Changes added : Added error code for invalid certificate

Problem : When Invalid certificate was passed to replace certificate
It was throwing error Internal error

Below is the redfishtool log:

redfishtool: Transport: Response Error: status_code: 500 --
Internal Error
redfishtool: raw: Error sending POST to resource, aborting

Solution : Return proper error code that is 400 -- Bad Request

Tested : I tested with redfish tool and below is the log
with correct error code

redfishtool: Transport: Response Error: status_code: 400 --
Bad Request
   redfishtool: raw: Error sending POST to resource, aborting

Change-Id: I71e7a72a4c156dc80321641f279c20aff4bd6df1
Signed-off-by: Chandramohan Harkude <chandramohan.harkude@gmail.com>
diff --git a/redfish-core/lib/certificate_service.hpp b/redfish-core/lib/certificate_service.hpp
index 68b7ff5..1f69123 100644
--- a/redfish-core/lib/certificate_service.hpp
+++ b/redfish-core/lib/certificate_service.hpp
@@ -465,6 +465,26 @@
                        "/Links/Certificates@odata.count"_json_pointer);
 }
 
+inline void handleError(const std::string_view dbusErrorName,
+                        const std::string& id, const std::string& certificate,
+                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
+{
+    if (dbusErrorName == "org.freedesktop.DBus.Error.UnknownObject")
+    {
+        messages::resourceNotFound(asyncResp->res, "Certificate", id);
+    }
+    else if (dbusErrorName ==
+             "xyz.openbmc_project.Certs.Error.InvalidCertificate")
+    {
+        messages::propertyValueIncorrect(asyncResp->res, "Certificate",
+                                         certificate);
+    }
+    else
+    {
+        messages::internalError(asyncResp->res);
+    }
+}
+
 inline void handleReplaceCertificateAction(
     App& app, const crow::Request& req,
     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
@@ -549,19 +569,21 @@
     std::shared_ptr<CertificateFile> certFile =
         std::make_shared<CertificateFile>(certificate);
     crow::connections::systemBus->async_method_call(
-        [asyncResp, certFile, objectPath, service, url{*parsedUrl}, id,
-         name](const boost::system::error_code& ec) {
+        [asyncResp, certFile, objectPath, service, url{*parsedUrl}, id, name,
+         certificate](const boost::system::error_code& ec,
+                      sdbusplus::message::message& m) {
             if (ec)
             {
                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
-                if (ec.value() ==
-                    boost::system::linux_error::bad_request_descriptor)
+                const sd_bus_error* dbusError = m.get_error();
+                if ((dbusError != nullptr) && (dbusError->name != nullptr))
                 {
-                    messages::resourceNotFound(asyncResp->res, "Certificate",
-                                               id);
-                    return;
+                    handleError(dbusError->name, id, certificate, asyncResp);
                 }
-                messages::internalError(asyncResp->res);
+                else
+                {
+                    messages::internalError(asyncResp->res);
+                }
                 return;
             }
             getCertificateProperties(asyncResp, objectPath, service, id, url,