Commit c29dd463ca87fd836b68498d019592e38a0cee4c
Committed by
Moritz Wirger
1 parent
b9111ceb
Add string parsing to exception error code, remove leading groups from createGroup response.
It is not clear from the documentation that the error code is a string, so both string and integer are accepted now.
Showing
2 changed files
with
23 additions
and
4 deletions
src/Hue.cpp
| ... | ... | @@ -197,10 +197,11 @@ std::string Hue::requestUsername() |
| 197 | 197 | } |
| 198 | 198 | else if (answer.size() > 0 && answer[0].count("error")) |
| 199 | 199 | { |
| 200 | + HueAPIResponseException exception = HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]); | |
| 200 | 201 | // All errors except 101: Link button not pressed |
| 201 | - if (utils::safeGetMember(answer, 0, "error", "type") != 101) | |
| 202 | + if (exception.GetErrorNumber() != 101) | |
| 202 | 203 | { |
| 203 | - throw HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]); | |
| 204 | + throw exception; | |
| 204 | 205 | } |
| 205 | 206 | } |
| 206 | 207 | |
| ... | ... | @@ -353,7 +354,14 @@ int Hue::createGroup(const CreateGroup& params) |
| 353 | 354 | nlohmann::json id = utils::safeGetMember(response, 0, "success", "id"); |
| 354 | 355 | if (id.is_string()) |
| 355 | 356 | { |
| 356 | - return std::stoi(id.get<std::string>()); | |
| 357 | + std::string idStr = id.get<std::string>(); | |
| 358 | + // Sometimes the response can be /groups/<id>? | |
| 359 | + if (idStr.find("/groups/") == 0) | |
| 360 | + { | |
| 361 | + idStr.erase(0, 8); | |
| 362 | + } | |
| 363 | + stateCache.refresh(); | |
| 364 | + return std::stoi(idStr); | |
| 357 | 365 | } |
| 358 | 366 | return 0; |
| 359 | 367 | } | ... | ... |
src/HueException.cpp
| ... | ... | @@ -74,7 +74,18 @@ const std::string& HueAPIResponseException::GetDescription() const noexcept |
| 74 | 74 | HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response) |
| 75 | 75 | { |
| 76 | 76 | const nlohmann::json error = response.at("error"); |
| 77 | - int errorCode = error.value("type", -1); | |
| 77 | + int errorCode = -1; | |
| 78 | + if (error.count("type")) | |
| 79 | + { | |
| 80 | + if (error["type"].is_number_integer()) | |
| 81 | + { | |
| 82 | + errorCode = error["type"].get<int>(); | |
| 83 | + } | |
| 84 | + else if (error["type"].is_string()) | |
| 85 | + { | |
| 86 | + errorCode = std::stoi(error["type"].get<std::string>()); | |
| 87 | + } | |
| 88 | + } | |
| 78 | 89 | std::string address = error.value("address", ""); |
| 79 | 90 | std::string description = error.value("description", ""); |
| 80 | 91 | return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(description)); | ... | ... |