From c29dd463ca87fd836b68498d019592e38a0cee4c Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Sat, 18 Apr 2020 18:44:48 +0200 Subject: [PATCH] Add string parsing to exception error code, remove leading groups from createGroup response. --- src/Hue.cpp | 14 +++++++++++--- src/HueException.cpp | 13 ++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Hue.cpp b/src/Hue.cpp index 0eae5e0..9088643 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -197,10 +197,11 @@ std::string Hue::requestUsername() } else if (answer.size() > 0 && answer[0].count("error")) { + HueAPIResponseException exception = HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]); // All errors except 101: Link button not pressed - if (utils::safeGetMember(answer, 0, "error", "type") != 101) + if (exception.GetErrorNumber() != 101) { - throw HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]); + throw exception; } } @@ -353,7 +354,14 @@ int Hue::createGroup(const CreateGroup& params) nlohmann::json id = utils::safeGetMember(response, 0, "success", "id"); if (id.is_string()) { - return std::stoi(id.get()); + std::string idStr = id.get(); + // Sometimes the response can be /groups/? + if (idStr.find("/groups/") == 0) + { + idStr.erase(0, 8); + } + stateCache.refresh(); + return std::stoi(idStr); } return 0; } diff --git a/src/HueException.cpp b/src/HueException.cpp index 4b10934..31a81a5 100644 --- a/src/HueException.cpp +++ b/src/HueException.cpp @@ -74,7 +74,18 @@ const std::string& HueAPIResponseException::GetDescription() const noexcept HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response) { const nlohmann::json error = response.at("error"); - int errorCode = error.value("type", -1); + int errorCode = -1; + if (error.count("type")) + { + if (error["type"].is_number_integer()) + { + errorCode = error["type"].get(); + } + else if (error["type"].is_string()) + { + errorCode = std::stoi(error["type"].get()); + } + } std::string address = error.value("address", ""); std::string description = error.value("description", ""); return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(description)); -- libgit2 0.21.4