From f11e0a1c1b74690a077caeb6aa7df2d79cfa284a Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 10 Dec 2024 15:47:15 -0300 Subject: [PATCH] Make handling of presence data consistent There was one place where we throw and one where we fail silently (even though the surrounding code throws for a bunch of other reasons of seemingly similar severity). So throw consistently. --- Sources/AblyChat/DefaultPresence.swift | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Sources/AblyChat/DefaultPresence.swift b/Sources/AblyChat/DefaultPresence.swift index 682e054..b11676f 100644 --- a/Sources/AblyChat/DefaultPresence.swift +++ b/Sources/AblyChat/DefaultPresence.swift @@ -198,13 +198,15 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy) } - private func decodePresenceData(from data: Any?) -> PresenceData? { - guard let userData = data as? [String: Any] else { - return nil + private func decodePresenceData(from data: Any?) throws -> PresenceData? { + guard let data = data as? [String: Any] else { + let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data") + logger.log(message: error.message, level: .error) + throw error } do { - let jsonData = try JSONSerialization.data(withJSONObject: userData, options: []) + let jsonData = try JSONSerialization.data(withJSONObject: data, options: []) let presenceData = try JSONDecoder().decode(PresenceData.self, from: jsonData) return presenceData } catch { @@ -221,11 +223,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { throw error } let presenceMembers = try members.map { member in - guard let data = member.data as? [String: Any] else { - let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data") - logger.log(message: error.message, level: .error) - throw error - } + let userCustomData = try decodePresenceData(from: member.data) guard let clientID = member.clientId else { let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without clientId") @@ -239,8 +237,6 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { throw error } - let userCustomData = decodePresenceData(from: data) - // Seems like we want to just forward on `extras` from the cocoa SDK but that is an `ARTJsonCompatible` type which is not `Sendable`... currently just converting this to a `Sendable` type (`String`) until we know what to do with this. let extras = member.extras?.toJSONString() @@ -271,7 +267,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { throw error } - let userCustomDataDecoded = decodePresenceData(from: message.data) + let userCustomDataDecoded = try decodePresenceData(from: message.data) let presenceEvent = PresenceEvent( action: event,