diff options
author | Kowalski, Kamil <kamil.kowalski@intel.com> | 2018-01-29 16:55:35 +0300 |
---|---|---|
committer | Ed tanous <ed@tanous.net> | 2018-04-22 21:56:24 +0300 |
commit | f4c4dcf438a43f04fe91f1350f88eda618cdb3e4 (patch) | |
tree | 930e48b7217086217575d631c96ebc3287d77d15 /redfish-core/scripts/error_messages/error_messages.cpp.in | |
parent | c7070ac2737212b0d82670e0af7ab81f10c8dbf9 (diff) | |
download | bmcweb-f4c4dcf438a43f04fe91f1350f88eda618cdb3e4.tar.xz |
Introduce Redfish Error messages definitions
Added a python script that should be use to generate error_messages.cpp na .hpp
files. These files as are not subject to change in a dynamic manner, should be
regenerated only when MessageRegistry changes.
Tested on x86 Ubuntu VM and Wolfpass platform:
a) No regression in BMCWeb interface
- Went through WebUI observed no changes in functionality
b) No regression in Redfish functionality
- Went through nodes and observed no changes in functionality
- Sessions now return error messages in addition to HTTP codes
Change-Id: I4aba9ee247b6cd2c46a9c158d14bdc7546e9b11b
Signed-off-by: Kowalski, Kamil <kamil.kowalski@intel.com>
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Diffstat (limited to 'redfish-core/scripts/error_messages/error_messages.cpp.in')
-rw-r--r-- | redfish-core/scripts/error_messages/error_messages.cpp.in | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/redfish-core/scripts/error_messages/error_messages.cpp.in b/redfish-core/scripts/error_messages/error_messages.cpp.in new file mode 100644 index 0000000000..a254b08c72 --- /dev/null +++ b/redfish-core/scripts/error_messages/error_messages.cpp.in @@ -0,0 +1,100 @@ +/* +// Copyright (c) 2018 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +*/ +#include <error_messages.hpp> +#include <crow/logging.h> + +namespace redfish { + +namespace messages { + +void addMessageToErrorJson(nlohmann::json& target, + const nlohmann::json& message) { + auto& error = target["error"]; + + // If this is the first error message, fill in the information from the first + // error message to the top level struct + if (!error.is_object()) { + auto message_id_iterator = message.find("MessageId"); + if (message_id_iterator == message.end()) { + CROW_LOG_CRITICAL << "Attempt to add error message without MessageId"; + return; + } + + auto message_field_iterator = message.find("Message"); + if (message_field_iterator == message.end()) { + CROW_LOG_CRITICAL << "Attempt to add error message without Message"; + return; + } + // clang-format off + error = { + {"code", *message_id_iterator}, + {"message", *message_field_iterator} + }; + // clang-format on + } else { + // More than 1 error occurred, so the message has to be generic + error["code"] = std::string(MESSAGE_VERSION_PREFIX) + "GeneralError"; + error["message"] = "A general error has occurred. See ExtendedInfo for more" + "information."; + } + + // This check could technically be done in 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& extended_info = error[messages::MESSAGE_ANNOTATION]; + if (!extended_info.is_array()) { + extended_info = nlohmann::json::array(); + } + + extended_info.push_back(message); +} + +void addMessageToJsonRoot(nlohmann::json& target, + const nlohmann::json& message) { + if (!target[messages::MESSAGE_ANNOTATION].is_array()) { + // Force object to be an array + target[messages::MESSAGE_ANNOTATION] = nlohmann::json::array(); + } + + target[messages::MESSAGE_ANNOTATION].push_back(message); +} + +void addMessageToJson(nlohmann::json& target, const nlohmann::json& message, + const std::string& fieldPath) { + nlohmann::json_pointer extendedInfo(fieldPath + messages::MESSAGE_ANNOTATION); + + if (!target[extendedInfo].is_array()) { + // Force object to be an array + target[extendedInfo] = nlohmann::json::array(); + } + + // Object exists and it is an array so we can just push in the message + target[extendedInfo].push_back(message); +} + +/********************************* + * AUTOGENERATED FUNCTIONS START * + *********************************/ + +/*<<<<<<AUTOGENERATED_FUNCTIONS_IMPLEMENTATIONS>>>>>>*/ + +/********************************* + * AUTOGENERATED FUNCTIONS END * + *********************************/ + +} // namespace messages + +} // namespace redifsh |