ios: fix decoder crashing (#3921)

* ios: fix ios decoder crashing

* change

* fix

* refactor

* simplify

---------

Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
This commit is contained in:
spaced4ndy
2024-03-18 18:38:49 +00:00
committed by GitHub
co-authored by Evgeny Poberezkin
parent 94a6f300ed
commit a3310cd62e
+10 -4
View File
@@ -213,13 +213,11 @@ public func chatResponse(_ s: String) -> ChatResponse {
}
} else if type == "chatCmdError" {
if let jError = jResp["chatCmdError"] as? NSDictionary {
let user: UserRef? = try? decodeObject(jError["user_"] as Any)
return .chatCmdError(user_: user, chatError: .invalidJSON(json: prettyJSON(jError) ?? ""))
return .chatCmdError(user_: decodeUser_(jError), chatError: .invalidJSON(json: prettyJSON(jError) ?? ""))
}
} else if type == "chatError" {
if let jError = jResp["chatError"] as? NSDictionary {
let user: UserRef? = try? decodeObject(jError["user_"] as Any)
return .chatError(user_: user, chatError: .invalidJSON(json: prettyJSON(jError) ?? ""))
return .chatError(user_: decodeUser_(jError), chatError: .invalidJSON(json: prettyJSON(jError) ?? ""))
}
}
}
@@ -228,6 +226,14 @@ public func chatResponse(_ s: String) -> ChatResponse {
return ChatResponse.response(type: type ?? "invalid", json: json ?? s)
}
private func decodeUser_(_ jDict: NSDictionary) -> UserRef? {
if let user_ = jDict["user_"] {
try? decodeObject(user_ as Any)
} else {
nil
}
}
func parseChatData(_ jChat: Any) throws -> ChatData {
let jChatDict = jChat as! NSDictionary
let chatInfo: ChatInfo = try decodeObject(jChatDict["chatInfo"]!)