1 #include "utils/dbus_utils.hpp"
2 
3 #include "async_resp.hpp"
4 
5 #include <boost/system/error_code.hpp>
6 #include <nlohmann/json.hpp>
7 #include <sdbusplus/message.hpp>
8 
9 #include <memory>
10 #include <string>
11 #include <string_view>
12 
13 namespace redfish
14 {
15 namespace details
16 {
17 
18 void afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19                       const std::string& redfishPropertyName,
20                       const nlohmann::json& propertyValue,
21                       const boost::system::error_code& ec,
22                       const sdbusplus::message_t& msg)
23 {
24     if (ec)
25     {
26         if (ec.value() == boost::system::errc::permission_denied)
27         {
28             messages::insufficientPrivilege(asyncResp->res);
29         }
30         const sd_bus_error* dbusError = msg.get_error();
31         if (dbusError != nullptr)
32         {
33             std::string_view errorName(dbusError->name);
34 
35             if (errorName == "xyz.openbmc_project.Common.Error.InvalidArgument")
36             {
37                 BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
38                 messages::propertyValueIncorrect(
39                     asyncResp->res, redfishPropertyName, propertyValue);
40                 return;
41             }
42             if (errorName ==
43                 "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")
44             {
45                 BMCWEB_LOG_WARNING(
46                     "BMC not ready, operation not allowed right now");
47                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
48                 return;
49             }
50             if (errorName == "xyz.openbmc_project.State.Host.Error.BMCNotReady")
51             {
52                 BMCWEB_LOG_WARNING(
53                     "BMC not ready, operation not allowed right now");
54                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
55                 return;
56             }
57             if (errorName == "xyz.openbmc_project.Common.Error.NotAllowed")
58             {
59                 messages::propertyNotWritable(asyncResp->res,
60                                               redfishPropertyName);
61                 return;
62             }
63         }
64         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
65                          redfishPropertyName, ec);
66         messages::internalError(asyncResp->res);
67         return;
68     }
69     // Only set 204 if another error hasn't already happened.
70     if (asyncResp->res.result() == boost::beast::http::status::ok)
71     {
72         asyncResp->res.result(boost::beast::http::status::no_content);
73     }
74 };
75 
76 void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77                             const std::string& redfishActionName,
78                             const std::string& redfishActionParameterName,
79                             const boost::system::error_code& ec,
80                             const sdbusplus::message_t& /*msg*/)
81 {
82     if (ec)
83     {
84         if (ec.value() == boost::asio::error::invalid_argument)
85         {
86             BMCWEB_LOG_WARNING(
87                 "Resource {} is patched with invalid argument during action {}",
88                 redfishActionParameterName, redfishActionName);
89             if (redfishActionParameterName.empty())
90             {
91                 messages::operationFailed(asyncResp->res);
92             }
93             else
94             {
95                 messages::actionParameterValueError(asyncResp->res,
96                                                     redfishActionParameterName,
97                                                     redfishActionName);
98             }
99             return;
100         }
101         if (ec.value() == boost::asio::error::host_unreachable)
102         {
103             BMCWEB_LOG_WARNING(
104                 "Resource {} is not found while performing action {}",
105                 redfishActionParameterName, redfishActionName);
106             messages::resourceNotFound(asyncResp->res, "Actions",
107                                        redfishActionName);
108             return;
109         }
110 
111         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
112                          redfishActionParameterName, ec);
113         messages::internalError(asyncResp->res);
114         return;
115     }
116     // Only set 204 if another error hasn't already happened.
117     if (asyncResp->res.result() == boost::beast::http::status::ok)
118     {
119         asyncResp->res.result(boost::beast::http::status::no_content);
120     }
121 };
122 } // namespace details
123 } // namespace redfish
124