diff options
author | Ed Tanous <edtanous@google.com> | 2022-06-27 22:59:12 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-06-13 20:48:37 +0300 |
commit | 2e8c4bda9c4b2809ca76bb227f818592515a3e4a (patch) | |
tree | c4c1532a1a5788c774d2b569136ffde66a6c9c72 | |
parent | e2616cc5d2093715bbbb5a7ff6aa0c86b2c466d4 (diff) | |
download | bmcweb-2e8c4bda9c4b2809ca76bb227f818592515a3e4a.tar.xz |
Make propertyValueTypeError more typesafe
Similar to the prior patchset in this series, propertyValueTypeError can
be moved to safer constructs. This ensures that we are minimizing how
many places we are calling dump() from, and allows us to reduce the
amount of code written for error handling.
Tested:
PATCH /redfish/v1/SessionService {"SessionTimeout": "foo"}
Returns PropertyValueTypeError in the same behavior as prior to this
patch.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Iddff4b787f35c49bf923663d61bba156687f358c
-rw-r--r-- | redfish-core/include/error_messages.hpp | 4 | ||||
-rw-r--r-- | redfish-core/include/utils/json_utils.hpp | 24 | ||||
-rw-r--r-- | redfish-core/lib/account_service.hpp | 8 | ||||
-rw-r--r-- | redfish-core/lib/ethernet.hpp | 14 | ||||
-rw-r--r-- | redfish-core/lib/hypervisor_system.hpp | 9 | ||||
-rw-r--r-- | redfish-core/src/error_messages.cpp | 8 |
6 files changed, 21 insertions, 46 deletions
diff --git a/redfish-core/include/error_messages.hpp b/redfish-core/include/error_messages.hpp index 99e9ae8f37..77aa8958f8 100644 --- a/redfish-core/include/error_messages.hpp +++ b/redfish-core/include/error_messages.hpp @@ -571,10 +571,10 @@ void operationTimeout(crow::Response& res); * @param[in] arg2 Parameter of message that will replace %2 in its body. * * @returns Message PropertyValueTypeError formatted to JSON */ -nlohmann::json propertyValueTypeError(std::string_view arg1, +nlohmann::json propertyValueTypeError(const nlohmann::json& arg1, std::string_view arg2); -void propertyValueTypeError(crow::Response& res, std::string_view arg1, +void propertyValueTypeError(crow::Response& res, const nlohmann::json& arg1, std::string_view arg2); /** diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index 7c701428af..6e18157fc0 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -219,20 +219,12 @@ bool unpackValue(nlohmann::json& jsonValue, std::string_view key, { if (!jsonValue.is_array()) { - messages::propertyValueTypeError( - res, - res.jsonValue.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - key); + messages::propertyValueTypeError(res, res.jsonValue, key); return false; } if (jsonValue.size() != value.size()) { - messages::propertyValueTypeError( - res, - res.jsonValue.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - key); + messages::propertyValueTypeError(res, res.jsonValue, key); return false; } size_t index = 0; @@ -247,11 +239,7 @@ bool unpackValue(nlohmann::json& jsonValue, std::string_view key, { if (!jsonValue.is_array()) { - messages::propertyValueTypeError( - res, - res.jsonValue.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - key); + messages::propertyValueTypeError(res, res.jsonValue, key); return false; } @@ -270,11 +258,7 @@ bool unpackValue(nlohmann::json& jsonValue, std::string_view key, { if (ec == UnpackErrorCode::invalidType) { - messages::propertyValueTypeError( - res, - jsonValue.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - key); + messages::propertyValueTypeError(res, jsonValue, key); } else if (ec == UnpackErrorCode::outOfRange) { diff --git a/redfish-core/lib/account_service.hpp b/redfish-core/lib/account_service.hpp index 5e9de4e5e6..6f5f122b00 100644 --- a/redfish-core/lib/account_service.hpp +++ b/redfish-core/lib/account_service.hpp @@ -393,11 +393,9 @@ inline void handleRoleMapPatch( else { BMCWEB_LOG_ERROR << "Can't delete the object"; - messages::propertyValueTypeError( - asyncResp->res, - thisJson.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - "RemoteRoleMapping/" + std::to_string(index)); + messages::propertyValueTypeError(asyncResp->res, thisJson, + "RemoteRoleMapping/" + + std::to_string(index)); return; } } diff --git a/redfish-core/lib/ethernet.hpp b/redfish-core/lib/ethernet.hpp index 1d2c988c7c..411659d248 100644 --- a/redfish-core/lib/ethernet.hpp +++ b/redfish-core/lib/ethernet.hpp @@ -1233,11 +1233,8 @@ inline void { if (input.empty()) { - messages::propertyValueTypeError( - asyncResp->res, - nlohmann::json(input).dump( - 2, ' ', true, nlohmann::json::error_handler_t::replace), - "IPv4StaticAddresses"); + messages::propertyValueTypeError(asyncResp->res, input, + "IPv4StaticAddresses"); return; } @@ -1438,11 +1435,8 @@ inline void handleIPv6StaticAddressesPatch( { if (input.empty()) { - messages::propertyValueTypeError( - asyncResp->res, - nlohmann::json(input).dump( - 2, ' ', true, nlohmann::json::error_handler_t::replace), - "IPv6StaticAddresses"); + messages::propertyValueTypeError(asyncResp->res, input, + "IPv6StaticAddresses"); return; } size_t entryIdx = 1; diff --git a/redfish-core/lib/hypervisor_system.hpp b/redfish-core/lib/hypervisor_system.hpp index c06bd1f870..5a7105fc3e 100644 --- a/redfish-core/lib/hypervisor_system.hpp +++ b/redfish-core/lib/hypervisor_system.hpp @@ -566,7 +566,7 @@ inline void handleHypervisorIPv4StaticPatch( { if ((!input.is_array()) || input.empty()) { - messages::propertyValueTypeError(asyncResp->res, input.dump(), + messages::propertyValueTypeError(asyncResp->res, input, "IPv4StaticAddresses"); return; } @@ -885,11 +885,8 @@ inline void handleHypervisorEthernetInterfacePatch( const nlohmann::json& ipv4Static = *ipv4StaticAddresses; if (ipv4Static.begin() == ipv4Static.end()) { - messages::propertyValueTypeError( - asyncResp->res, - ipv4Static.dump(2, ' ', true, - nlohmann::json::error_handler_t::replace), - "IPv4StaticAddresses"); + messages::propertyValueTypeError(asyncResp->res, ipv4Static, + "IPv4StaticAddresses"); return; } diff --git a/redfish-core/src/error_messages.cpp b/redfish-core/src/error_messages.cpp index fcb8bcba40..abc817d443 100644 --- a/redfish-core/src/error_messages.cpp +++ b/redfish-core/src/error_messages.cpp @@ -979,14 +979,16 @@ void operationTimeout(crow::Response& res) * See header file for more information * @endinternal */ -nlohmann::json propertyValueTypeError(std::string_view arg1, +nlohmann::json propertyValueTypeError(const nlohmann::json& arg1, std::string_view arg2) { + std::string arg1Str = arg1.dump(2, ' ', true, + nlohmann::json::error_handler_t::replace); return getLog(redfish::registries::base::Index::propertyValueTypeError, - std::to_array({arg1, arg2})); + std::to_array<std::string_view>({arg1Str, arg2})); } -void propertyValueTypeError(crow::Response& res, std::string_view arg1, +void propertyValueTypeError(crow::Response& res, const nlohmann::json& arg1, std::string_view arg2) { res.result(boost::beast::http::status::bad_request); |