| 10 | |
| 11 | namespace CesiumClientCommon { |
| 12 | bool parseErrorResponse( |
| 13 | const std::span<const std::byte>& body, |
| 14 | std::string& outError, |
| 15 | std::string& outErrorDesc) { |
| 16 | rapidjson::Document doc; |
| 17 | doc.Parse(reinterpret_cast<const char*>(body.data()), body.size()); |
| 18 | |
| 19 | if (doc.HasParseError() || !doc.IsObject()) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | rapidjson::Value::Object rootObj = doc.GetObject(); |
| 24 | |
| 25 | const auto errorMember = doc.FindMember("error"); |
| 26 | if (errorMember != doc.MemberEnd() && errorMember->value.IsObject()) { |
| 27 | rootObj = errorMember->value.GetObject(); |
| 28 | outError = |
| 29 | CesiumUtility::JsonHelpers::getStringOrDefault(rootObj, "code", ""); |
| 30 | outErrorDesc = |
| 31 | CesiumUtility::JsonHelpers::getStringOrDefault(rootObj, "message", ""); |
| 32 | } else { |
| 33 | outError = |
| 34 | CesiumUtility::JsonHelpers::getStringOrDefault(rootObj, "error", ""); |
| 35 | outErrorDesc = CesiumUtility::JsonHelpers::getStringOrDefault( |
| 36 | rootObj, |
| 37 | "error_description", |
| 38 | ""); |
| 39 | } |
| 40 | |
| 41 | if (outError.empty() && outErrorDesc.empty()) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | const auto& detailsMember = rootObj.FindMember("details"); |
| 46 | if (detailsMember != rootObj.MemberEnd() && detailsMember->value.IsArray()) { |
| 47 | for (const auto& value : detailsMember->value.GetArray()) { |
| 48 | const std::string code = |
| 49 | CesiumUtility::JsonHelpers::getStringOrDefault(value, "code", ""); |
| 50 | const std::string message = |
| 51 | CesiumUtility::JsonHelpers::getStringOrDefault(value, "message", ""); |
| 52 | const std::string target = |
| 53 | CesiumUtility::JsonHelpers::getStringOrDefault(value, "target", ""); |
| 54 | if (target.empty()) { |
| 55 | outErrorDesc += fmt::format("\n\t - {}: {}", code, message); |
| 56 | } else { |
| 57 | outErrorDesc += |
| 58 | fmt::format("\n\t - {} in {}: {}", code, target, message); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | } // namespace CesiumClientCommon |
no test coverage detected