summaryrefslogtreecommitdiff
path: root/redfish-core/src/utils
diff options
context:
space:
mode:
authorJason M. Bills <jason.m.bills@linux.intel.com>2018-10-09 22:45:45 +0300
committerJason M. Bills <jason.m.bills@linux.intel.com>2018-10-23 03:00:16 +0300
commitf12894f8bd7fc26ffa16e5a89072e6c19095f866 (patch)
treee2e2e1629f80a29efca04bd9714525465575549e /redfish-core/src/utils
parent9712f8ac42746e65f32c2bc3de0835846652568e (diff)
downloadbmcweb-f12894f8bd7fc26ffa16e5a89072e6c19095f866.tar.xz
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 <jason.m.bills@linux.intel.com>
Diffstat (limited to 'redfish-core/src/utils')
-rw-r--r--redfish-core/src/utils/json_utils.cpp482
1 files changed, 1 insertions, 481 deletions
diff --git a/redfish-core/src/utils/json_utils.cpp b/redfish-core/src/utils/json_utils.cpp
index e14131713a..a47b4d8217 100644
--- a/redfish-core/src/utils/json_utils.cpp
+++ b/redfish-core/src/utils/json_utils.cpp
@@ -21,484 +21,6 @@ namespace redfish
namespace json_util
{
-Result getString(const char* fieldName, const nlohmann::json& json,
- const std::string*& output)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- output = fieldIt->get_ptr<const std::string*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (output == nullptr)
- {
- return Result::WRONG_TYPE;
- }
-
- return Result::SUCCESS;
-}
-
-Result getObject(const char* fieldName, const nlohmann::json& json,
- nlohmann::json* output)
-{
- // Verify input pointer
- if (output == nullptr)
- {
- return Result::NULL_POINTER;
- }
-
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- // Verify type
- if (!fieldIt->is_object())
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- *output = *fieldIt;
-
- return Result::SUCCESS;
-}
-
-Result getArray(const char* fieldName, const nlohmann::json& json,
- nlohmann::json* output)
-{
- // Verify input pointer
- if (output == nullptr)
- {
- return Result::NULL_POINTER;
- }
-
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- // Verify type
- if (!fieldIt->is_array())
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- *output = *fieldIt;
-
- return Result::SUCCESS;
-}
-
-Result getInt(const char* fieldName, const nlohmann::json& json,
- int64_t& output)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- const int64_t* retVal = fieldIt->get_ptr<const int64_t*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getUnsigned(const char* fieldName, const nlohmann::json& json,
- uint64_t& output)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- const uint64_t* retVal = fieldIt->get_ptr<const uint64_t*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getBool(const char* fieldName, const nlohmann::json& json, bool& output)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- const bool* retVal = fieldIt->get_ptr<const bool*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getDouble(const char* fieldName, const nlohmann::json& json,
- double& output)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- return Result::NOT_EXIST;
- }
-
- const double* retVal = fieldIt->get_ptr<const double*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return 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)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- output = fieldIt->get_ptr<const std::string*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (output == nullptr)
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- return Result::SUCCESS;
-}
-
-Result getObject(const char* fieldName, const nlohmann::json& json,
- nlohmann::json* output, uint8_t msgCfgMap,
- nlohmann::json& msgJson, const std::string&& fieldPath)
-{
- // Verify input pointer
- if (output == nullptr)
- {
- return Result::NULL_POINTER;
- }
-
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- // Verify type
- if (!fieldIt->is_object())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- *output = *fieldIt;
-
- return Result::SUCCESS;
-}
-
-Result getArray(const char* fieldName, const nlohmann::json& json,
- nlohmann::json* output, uint8_t msgCfgMap,
- nlohmann::json& msgJson, const std::string&& fieldPath)
-{
- // Verify input pointer
- if (output == nullptr)
- {
- return Result::NULL_POINTER;
- }
-
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- // Verify type
- if (!fieldIt->is_array())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- *output = *fieldIt;
-
- return Result::SUCCESS;
-}
-
-Result getInt(const char* fieldName, const nlohmann::json& json,
- int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
- const std::string&& fieldPath)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- const int64_t* retVal = fieldIt->get_ptr<const int64_t*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getUnsigned(const char* fieldName, const nlohmann::json& json,
- uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
- const std::string&& fieldPath)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- const uint64_t* retVal = fieldIt->get_ptr<const uint64_t*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getBool(const char* fieldName, const nlohmann::json& json, bool& output,
- uint8_t msgCfgMap, nlohmann::json& msgJson,
- const std::string&& fieldPath)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- const bool* retVal = fieldIt->get_ptr<const bool*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
-Result getDouble(const char* fieldName, const nlohmann::json& json,
- double& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
- const std::string&& fieldPath)
-{
- // Find field
- auto fieldIt = json.find(fieldName);
-
- // Verify existence
- if (fieldIt == json.end())
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::MISSING))
- {
- messages::addMessageToJson(
- msgJson, messages::propertyMissing(fieldName), fieldPath);
- }
-
- return Result::NOT_EXIST;
- }
-
- const double* retVal = fieldIt->get_ptr<const double*>();
-
- // Verify type - we know that it exists, so nullptr means wrong type
- if (retVal == nullptr)
- {
- if (msgCfgMap & static_cast<int>(MessageSetting::TYPE_ERROR))
- {
- messages::addMessageToJson(
- msgJson,
- messages::propertyValueTypeError(fieldIt->dump(), fieldName),
- fieldPath);
- }
-
- return Result::WRONG_TYPE;
- }
-
- // Extract value
- output = *retVal;
-
- return Result::SUCCESS;
-}
-
bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
nlohmann::json& reqJson)
{
@@ -506,10 +28,8 @@ bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
if (reqJson.is_discarded())
{
- messages::addMessageToErrorJson(res.jsonValue,
- messages::malformedJSON());
+ messages::malformedJSON(res);
- res.result(boost::beast::http::status::bad_request);
res.end();
return false;