summaryrefslogtreecommitdiff
path: root/redfish-core/scripts/error_messages/error_messages.cpp.in
blob: a254b08c72bb7004e55bece3cf61e41ab8e5cd1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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