diff options
author | Nan Zhou <nanzhoumails@gmail.com> | 2022-08-12 21:05:09 +0300 |
---|---|---|
committer | Nan Zhou <nanzhoumails@gmail.com> | 2022-09-21 01:13:00 +0300 |
commit | 3590bd1dee5a0e56d4d282f0a1e3cf2c8804dfa1 (patch) | |
tree | 24dfa423506c14bbef29451d09b366fdfe7a5317 /redfish-core/src | |
parent | 6e5a32691a861ef54f7b2e9abd90181a369085d6 (diff) | |
download | bmcweb-3590bd1dee5a0e56d4d282f0a1e3cf2c8804dfa1.tar.xz |
query: propogate errors for expand
The existing code doesn't propogate errors of subqueries correctly.
This commit corrects the behavior, so that the final response gets all
error message of subqueries and the "highest priority" HTTP code.
DMTF doesn't specify how expand queries handle error codes, since using
subqueries is an implementation choice that we made in this project.
What we did here follows existing behavior of this project, and follows
the error message section of the Redfish spec;
[1] https://redfish.dmtf.org/schemas/DSP0266_1.15.1.html#error-responses
As for now, this commit uses the worst HTTP code among all the error
code. See query_param.hpp, function |propogateErrorCode| for detailed
order of the errror codes.
Tested:
1. this is difficult to test, but I hijacked the code so it returns
errors in TaskServices, then I verified that "/redfish/v1?$expand=."
correctly returns 500 and the gets the error message set.
2. unit test so that when there are multiple errors, the final response
gets a generate error message.
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I0c1ebdd9015f389801db9150d687027485f1203c
Diffstat (limited to 'redfish-core/src')
-rw-r--r-- | redfish-core/src/error_messages.cpp | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/redfish-core/src/error_messages.cpp b/redfish-core/src/error_messages.cpp index 198e54425d..1bfce24504 100644 --- a/redfish-core/src/error_messages.cpp +++ b/redfish-core/src/error_messages.cpp @@ -74,7 +74,7 @@ static void addMessageToErrorJson(nlohmann::json& target, "information on how to resolve the error."; } - // This check could technically be done in in the default construction + // This check could technically be done in the default construction // branch above, but because we need the pointer to the extended info field // anyway, it's more efficient to do it here. auto& extendedInfo = error[messages::messageAnnotation]; @@ -86,6 +86,39 @@ static void addMessageToErrorJson(nlohmann::json& target, extendedInfo.push_back(message); } +void moveErrorsToErrorJson(nlohmann::json& target, nlohmann::json& source) +{ + if (!source.is_object()) + { + return; + } + auto errorIt = source.find("error"); + if (errorIt == source.end()) + { + // caller puts error message in root + messages::addMessageToErrorJson(target, source); + source.clear(); + return; + } + auto extendedInfoIt = errorIt->find(messages::messageAnnotation); + if (extendedInfoIt == errorIt->end()) + { + return; + } + const nlohmann::json::array_t* extendedInfo = + (*extendedInfoIt).get_ptr<const nlohmann::json::array_t*>(); + if (extendedInfo == nullptr) + { + source.erase(errorIt); + return; + } + for (const nlohmann::json& message : *extendedInfo) + { + addMessageToErrorJson(target, message); + } + source.erase(errorIt); +} + static void addMessageToJsonRoot(nlohmann::json& target, const nlohmann::json& message) { |