mirror of
https://github.com/yuzu-emu/discord-rpc.git
synced 2024-11-27 20:54:26 +01:00
Do/wrap error checking around json reading
This commit is contained in:
parent
6774b5d881
commit
ef60ec40c3
@ -100,20 +100,16 @@ extern "C" void Discord_UpdateConnection()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* evtName = nullptr;
|
const char* evtName = GetStrMember(&message, "evt");
|
||||||
auto evt = message.FindMember("evt");
|
const char* nonce = GetStrMember(&message, "nonce");
|
||||||
if (evt != message.MemberEnd() && evt->value.IsString()) {
|
|
||||||
evtName = evt->value.GetString();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto nonce = message.FindMember("nonce");
|
if (nonce) {
|
||||||
if (nonce != message.MemberEnd() && nonce->value.IsString()) {
|
|
||||||
// in responses only -- should use to match up response when needed.
|
// in responses only -- should use to match up response when needed.
|
||||||
|
|
||||||
if (evtName && strcmp(evtName, "ERROR") == 0) {
|
if (evtName && strcmp(evtName, "ERROR") == 0) {
|
||||||
auto data = message.FindMember("data");
|
auto data = GetObjMember(&message, "data");
|
||||||
LastErrorCode = data->value["code"].GetInt();
|
LastErrorCode = GetIntMember(data, "code");
|
||||||
StringCopy(LastErrorMessage, data->value["message"].GetString());
|
StringCopy(LastErrorMessage, GetStrMember(data, "message", ""));
|
||||||
GotErrorMessage.store(true);
|
GotErrorMessage.store(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,16 +120,20 @@ extern "C" void Discord_UpdateConnection()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(evtName, "GAME_JOIN") == 0) {
|
if (strcmp(evtName, "GAME_JOIN") == 0) {
|
||||||
auto data = message.FindMember("data");
|
auto data = GetObjMember(&message, "data");
|
||||||
auto secret = data->value["secret"].GetString();
|
auto secret = GetStrMember(data, "secret");
|
||||||
StringCopy(JoinGameSecret, secret);
|
if (secret) {
|
||||||
WasJoinGame.store(true);
|
StringCopy(JoinGameSecret, secret);
|
||||||
|
WasJoinGame.store(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (strcmp(evtName, "GAME_SPECTATE") == 0) {
|
else if (strcmp(evtName, "GAME_SPECTATE") == 0) {
|
||||||
auto data = message.FindMember("data");
|
auto data = GetObjMember(&message, "data");
|
||||||
auto secret = data->value["secret"].GetString();
|
auto secret = GetStrMember(data, "secret");
|
||||||
StringCopy(SpectateGameSecret, secret);
|
if (secret) {
|
||||||
WasSpectateGame.store(true);
|
StringCopy(SpectateGameSecret, secret);
|
||||||
|
WasSpectateGame.store(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,9 +114,8 @@ bool RpcConnection::Read(JsonDocument& message)
|
|||||||
switch (readFrame.opcode) {
|
switch (readFrame.opcode) {
|
||||||
case Opcode::Close: {
|
case Opcode::Close: {
|
||||||
message.ParseInsitu(readFrame.message);
|
message.ParseInsitu(readFrame.message);
|
||||||
lastErrorCode = message["code"].GetInt();
|
lastErrorCode = GetIntMember(&message, "code");
|
||||||
const auto& m = message["message"];
|
StringCopy(lastErrorMessage, GetStrMember(&message, "message", ""));
|
||||||
StringCopy(lastErrorMessage, m.GetString());
|
|
||||||
Close();
|
Close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -142,3 +142,40 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using JsonValue = rapidjson::GenericValue<UTF8, PoolAllocator>;
|
||||||
|
|
||||||
|
inline JsonValue* GetObjMember(JsonValue* obj, const char* name)
|
||||||
|
{
|
||||||
|
if (obj) {
|
||||||
|
auto member = obj->FindMember(name);
|
||||||
|
if (member != obj->MemberEnd() && member->value.IsObject()) {
|
||||||
|
return &member->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int GetIntMember(JsonValue* obj, const char* name, int notFoundDefault = 0)
|
||||||
|
{
|
||||||
|
if (obj) {
|
||||||
|
auto member = obj->FindMember(name);
|
||||||
|
if (member != obj->MemberEnd() && member->value.IsInt()) {
|
||||||
|
return member->value.GetInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return notFoundDefault;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const char* GetStrMember(JsonValue* obj,
|
||||||
|
const char* name,
|
||||||
|
const char* notFoundDefault = nullptr)
|
||||||
|
{
|
||||||
|
if (obj) {
|
||||||
|
auto member = obj->FindMember(name);
|
||||||
|
if (member != obj->MemberEnd() && member->value.IsString()) {
|
||||||
|
return member->value.GetString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return notFoundDefault;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user