MCPcopy Create free account
hub / github.com/LagPixelLOL/ChatGPTCLIBot / check_err_obj

Function check_err_obj

main/interface/network/APIKey.cpp:123–175  ·  view source on GitHub ↗

* Check if the json object has an error object, if so, print the error message and set the status. * @param json_to_check The json object to check. * @param status_in The status object to modify. * @param print_err_msg Whether to print the error message. * @return True if the json object has an error object, false otherwise. */

Source from the content-addressed store, hash-verified

121 * @return True if the json object has an error object, false otherwise.
122 */
123 bool check_err_obj(const nlohmann::json& json_to_check, APIKeyStatus& status_in, const bool& print_err_msg) {
124 status_in = APIKeyStatus::VALID;
125 auto it_err = json_to_check.find("error");
126 nlohmann::json::const_iterator it_status;
127 if (it_err != json_to_check.end() && it_err->is_object()) {
128 status_in = APIKeyStatus::API_REQUEST_FAILED;
129 std::optional<std::string> message;
130 auto it_message = it_err->find("message");
131 if (it_message != it_err->end() && it_message->is_string()) {
132 message = it_message->get<std::string>();
133 }
134 auto it_code = it_err->find("code");
135 nlohmann::json::const_iterator it_type;
136 nlohmann::json::const_iterator it_discord;
137 if (it_code != it_err->end() && it_code->is_string()) {
138 std::string error_code = it_code->get<std::string>();
139 if (error_code == "invalid_api_key" || error_code == "account_deactivated") {
140 status_in = APIKeyStatus::INVALID_KEY;
141 } else if (!print_err_msg) {
142 util::println_err("API returned error code. Code: " + error_code);
143 }
144 } else if ((it_type = it_err->find("type")) != it_err->end() && it_type->is_string()) {
145 std::string error_type = it_type->get<std::string>();
146 if (error_type == "insufficient_quota") {
147 status_in = APIKeyStatus::QUOTA_EXCEEDED;
148 } else if (error_type == "tokens" || error_type == "requests") {
149 status_in = APIKeyStatus::RATE_LIMIT_REACHED;
150 } else if (!print_err_msg) {
151 util::println_err("API returned error type. Type: " + error_type);
152 }
153 } else if ((it_discord = it_err->find("discord")) != it_err->end() && it_discord->is_string() && message) {
154 if (boost::starts_with(*message, "CATTO: Your API KEY is INVALID")) {
155 status_in = APIKeyStatus::INVALID_KEY;
156 }
157 } else {
158 util::println_err("API returned unknown error. Json: " + json_to_check.dump(2));
159 }
160 if (print_err_msg && message) {
161 util::println_err("\nAPI returned error: " + (status_in != APIKeyStatus::INVALID_KEY ? *message : "The API key is invalid."));
162 }
163 return true;
164 } else if ((it_status = json_to_check.find("status")) != json_to_check.end() && it_status->is_boolean() && !it_status->get<bool>()) {
165 status_in = APIKeyStatus::API_REQUEST_FAILED;
166 if (print_err_msg) {
167 auto it_response = json_to_check.find("response");
168 if (it_response != json_to_check.end() && it_response->is_string()) {
169 util::println_err("\nAPI returned error: " + it_response->get<std::string>());
170 }
171 }
172 return true;
173 }
174 return false;
175 }
176} // api

Callers 5

translateFunction · 0.85
start_loopFunction · 0.85
downloadFunction · 0.85
check_keyFunction · 0.85
get_embeddingsFunction · 0.85

Calls 2

println_errFunction · 0.85
endMethod · 0.45

Tested by

no test coverage detected