From f12894f8bd7fc26ffa16e5a89072e6c19095f866 Mon Sep 17 00:00:00 2001 From: "Jason M. Bills" Date: Tue, 9 Oct 2018 12:45:45 -0700 Subject: Improve the Redfish error reporting interface Makes the Redfish error reporting interface automatically handle setting the http status and JSON content in the response object. When using an AsyncResp object, this allows for simply calling the Redfish error and returning. Change-Id: Icfdce2de763225f070e8dd61e591f296703f46bb Signed-off-by: Jason M. Bills --- redfish-core/include/utils/json_utils.hpp | 279 +----------------------------- 1 file changed, 5 insertions(+), 274 deletions(-) (limited to 'redfish-core/include/utils') diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index 3263c73..b8195c4 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -27,264 +27,6 @@ namespace redfish namespace json_util { -/** - * @brief Defines JSON utils operation status - */ -enum class Result -{ - SUCCESS, - NOT_EXIST, - WRONG_TYPE, - NULL_POINTER -}; - -/** - * @brief Describes JSON utils messages requirement - */ -enum class MessageSetting -{ - NONE = 0x0, ///< No messages will be added - MISSING = 0x1, ///< PropertyMissing message will be added - TYPE_ERROR = 0x2 ///< PropertyValueTypeError message will be added -}; - -/** - * @brief Wrapper function for extracting string from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getString(const char* fieldName, const nlohmann::json& json, - const std::string*& output); - -/** - * @brief Wrapper function for extracting object from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getObject(const char* fieldName, const nlohmann::json& json, - nlohmann::json* output); - -/** - * @brief Wrapper function for extracting array from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getArray(const char* fieldName, const nlohmann::json& json, - nlohmann::json* output); - -/** - * @brief Wrapper function for extracting int from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getInt(const char* fieldName, const nlohmann::json& json, - int64_t& output); - -/** - * @brief Wrapper function for extracting uint from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getUnsigned(const char* fieldName, const nlohmann::json& json, - uint64_t& output); - -/** - * @brief Wrapper function for extracting bool from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getBool(const char* fieldName, const nlohmann::json& json, bool& output); - -/** - * @brief Wrapper function for extracting float from JSON object without - * throwing exceptions (nlohmann stores JSON floats as C++ doubles) - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getDouble(const char* fieldName, const nlohmann::json& json, - double& output); - -/** - * @brief Wrapper function for extracting string from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getString(const char* fieldName, const nlohmann::json& json, - const std::string*& output, uint8_t msgCfgMap, - nlohmann::json& msgJson, const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting object from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getObject(const char* fieldName, const nlohmann::json& json, - nlohmann::json* output, uint8_t msgCfgMap, - nlohmann::json& msgJson, const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting array from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getArray(const char* fieldName, const nlohmann::json& json, - nlohmann::json* output, uint8_t msgCfgMap, - nlohmann::json& msgJson, const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting int from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getInt(const char* fieldName, const nlohmann::json& json, - int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson, - const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting uint from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getUnsigned(const char* fieldName, const nlohmann::json& json, - uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson, - const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting bool from JSON object without - * throwing exceptions - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getBool(const char* fieldName, const nlohmann::json& json, bool& output, - uint8_t msgCfgMap, nlohmann::json& msgJson, - const std::string&& fieldPath); - -/** - * @brief Wrapper function for extracting float from JSON object without - * throwing exceptions (nlohmann stores JSON floats as C++ doubles) - * - * @param[in] fieldName Name of requested field - * @param[in] json JSON object from which field should be extracted - * @param[out] output Variable to which extracted will be written in case - * of success - * @param[in] msgCfgMap Map for message addition settings - * @param[out] msgJson JSON to which error messages will be added - * @param[in] fieldPath Field path in JSON - * - * @return Result informing about operation status, output will be - * written only in case of Result::SUCCESS - */ -Result getDouble(const char* fieldName, const nlohmann::json& json, - double& output, uint8_t msgCfgMap, nlohmann::json& msgJson, - const std::string&& fieldPath); - /** * @brief Processes request to extract JSON from its body. If it fails, adds * MalformedJSON message to response and ends it. @@ -318,9 +60,7 @@ void readJsonValues(const std::string& key, nlohmann::json& jsonValue, crow::Response& res, std::bitset& handled) { BMCWEB_LOG_DEBUG << "Unable to find variable for key" << key; - messages::addMessageToErrorJson(res.jsonValue, - messages::propertyUnknown(key)); - res.result(boost::beast::http::status::bad_request); + messages::propertyUnknown(res, key, key); } template & handled, crow::Response& res, { if (!handled.test(Index) && unpackValue::isRequired::value) { - messages::addMessageToErrorJson(res.jsonValue, - messages::propertyMissing(key)); - res.result(boost::beast::http::status::bad_request); + messages::propertyMissing(res, key, key); } details::handleMissing(handled, res, in...); } @@ -388,17 +122,14 @@ bool readJson(const crow::Request& req, crow::Response& res, const char* key, if (!jsonRequest.is_object()) { BMCWEB_LOG_DEBUG << "Json value is not an object"; - messages::addMessageToErrorJson(res.jsonValue, - messages::unrecognizedRequestBody()); - res.result(boost::beast::http::status::bad_request); + messages::unrecognizedRequestBody(res); return false; } if (jsonRequest.empty()) { BMCWEB_LOG_DEBUG << "Json value is empty"; - messages::addMessageToErrorJson(res.jsonValue, messages::emptyJSON()); - res.result(boost::beast::http::status::bad_request); + messages::emptyJSON(res); return false; } -- cgit v1.2.3