Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions Sources/AblyChat/ChatAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ internal final class ChatAPI {

// (CHA-MR4) Users should be able to send a reaction to a message via the `send` method of the `MessagesReactions` object
internal func sendReactionToMessage(_ messageSerial: String, roomName: String, params: SendMessageReactionParams) async throws(ErrorInfo) -> MessageReactionResponse {
// (CHA-MR4a1) If the serial passed to this method is invalid: undefined, null, empty string, an error with code 40000 must be thrown.
// (CHA-MR4a2) If the serial passed to this method is invalid: undefined, null, empty string, an error with code InvalidArgument must be thrown.
guard !messageSerial.isEmpty else {
throw ChatError.messageReactionInvalidMessageSerial.toErrorInfo()
throw InternalError.sendMessageReactionEmptyMessageSerial.toErrorInfo()
}

let endpoint = messageUrl(roomName: roomName, serial: messageSerial, suffix: "/reactions")
Expand All @@ -156,9 +156,9 @@ internal final class ChatAPI {

// (CHA-MR11) Users should be able to delete a reaction from a message via the `delete` method of the `MessagesReactions` object
internal func deleteReactionFromMessage(_ messageSerial: String, roomName: String, params: DeleteMessageReactionParams) async throws(ErrorInfo) -> MessageReactionResponse {
// (CHA-MR11a1) If the serial passed to this method is invalid: undefined, null, empty string, an error with code 40000 must be thrown.
// (CHA-MR11a2) If the serial passed to this method is invalid: undefined, null, empty string, an error with code InvalidArgument must be thrown.
guard !messageSerial.isEmpty else {
throw ChatError.messageReactionInvalidMessageSerial.toErrorInfo()
throw InternalError.deleteMessageReactionEmptyMessageSerial.toErrorInfo()
}

let endpoint = messageUrl(roomName: roomName, serial: messageSerial, suffix: "/reactions")
Expand Down Expand Up @@ -193,7 +193,7 @@ internal final class ChatAPI {
}
}

private func makeRequest<Response: JSONDecodable>(_ url: String, method: String, params: [String: String]? = nil, body: RequestBody? = nil) async throws(ErrorInfo) -> Response {
private func makeRequest<Response: JSONDecodable>(_ path: String, method: String, params: [String: String]? = nil, body: RequestBody? = nil) async throws(ErrorInfo) -> Response {
let ablyCocoaBody: Any? = if let body {
switch body {
case let .jsonObject(jsonObject):
Expand All @@ -206,10 +206,10 @@ internal final class ChatAPI {
}

// (CHA-M3e & CHA-M8d & CHA-M9c) If an error is returned from the REST API, its ErrorInfo representation shall be thrown as the result of the send call.
let paginatedResponse = try await realtime.request(method, path: url, params: params, body: ablyCocoaBody, headers: [:])
let paginatedResponse = try await realtime.request(method, path: path, params: params, body: ablyCocoaBody, headers: [:])

guard let firstItem = paginatedResponse.items.first else {
throw ChatError.noItemInResponse.toErrorInfo()
throw InternalError.noItemInResponse(path: path).toErrorInfo()
}

let jsonValue = JSONValue(ablyCocoaData: firstItem)
Expand All @@ -227,11 +227,4 @@ internal final class ChatAPI {
}
return paginatedResponse.toPaginatedResult(items: items)
}

internal enum ChatError: Error {
case noItemInResponse
case messageReactionInvalidMessageSerial
case messageReactionTypeRequired
case messageReactionNameRequired
}
}
3 changes: 2 additions & 1 deletion Sources/AblyChat/DefaultMessageReactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal final class DefaultMessageReactions: MessageReactions {
internal func delete(fromMessageWithSerial messageSerial: String, params: DeleteMessageReactionParams) async throws(ErrorInfo) {
let reactionType = params.type ?? options.defaultMessageReactionType
if reactionType != .unique, params.name == nil {
// CHA-MR11b1
throw InternalError.unableDeleteReactionWithoutName(reactionType: reactionType.rawValue).toErrorInfo()
}
let apiParams: ChatAPI.DeleteMessageReactionParams = .init(
Expand Down Expand Up @@ -94,7 +95,7 @@ internal final class DefaultMessageReactions: MessageReactions {
internal func subscribeRaw(_ callback: @escaping @MainActor @Sendable (MessageReactionRawEvent) -> Void) -> DefaultSubscription {
logger.log(message: "Subscribing to reaction events", level: .debug)
guard options.rawMessageReactions else {
// (CHA-MR7a) The attempt to subscribe to raw message reactions must throw an ErrorInfo with code 40000 and status code 400 if the room is not configured to support raw message reactions
// CHA-MR7c
// I'm replacing throwing with `fatalError` because it's a programmer error to call this method with invalid options.
fatalError("Room is not configured to support raw message reactions")
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/AblyChat/DefaultMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal final class DefaultMessages: Messages {
roomName: roomName,
subscriptionStartSerial: { [weak self] () throws(ErrorInfo) in
guard let self else {
throw MessagesError.noReferenceToSelf.toErrorInfo()
throw InternalError.failedToResolveSubscriptionPointBecauseMessagesInstanceGone.toErrorInfo()
Comment thread
maratal marked this conversation as resolved.
}
if channel.state == .attached, let startSerial = subscriptionPoints[uuid] {
return startSerial
Expand Down Expand Up @@ -159,11 +159,10 @@ internal final class DefaultMessages: Messages {
continuation.resume(returning: .success(subscriptionPoint))
} else {
logger.log(message: "Channel is attached, but attachSerial is not defined", level: .error)
continuation.resume(returning: .failure(InternalError.attachSerialIsNotDefined.toErrorInfo()))
continuation.resume(returning: .failure(InternalError.failedToResolveSubscriptionPointBecauseAttachSerialNotDefined.toErrorInfo()))
}
case .failed, .suspended:
// TODO: Revisit as part of https://github.com/ably-labs/ably-chat-swift/issues/32
let error = InternalError.channelFailedToAttach(cause: stateChange.reason)
let error = InternalError.failedToResolveSubscriptionPointBecauseChannelFailedToAttach(cause: stateChange.reason)
logger.log(message: "\(error)", level: .error)
continuation.resume(returning: .failure(error.toErrorInfo()))
default:
Expand Down
12 changes: 6 additions & 6 deletions Sources/AblyChat/DefaultPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR6b to CHA-PR6f
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence get operation: \(error)", level: .error)
throw error
Expand All @@ -41,7 +41,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR6b to CHA-PR6f
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence get operation: \(error)", level: .error)
throw error
Expand All @@ -63,7 +63,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR6b to CHA-PR6f
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence get operation: \(error)", level: .error)
throw error
Expand Down Expand Up @@ -94,7 +94,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR3c to CHA-PR3g
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence enter operation: \(error)", level: .error)
throw error
Expand Down Expand Up @@ -122,7 +122,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR10c to CHA-PR10g
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence update operation: \(error)", level: .error)
throw error
Expand Down Expand Up @@ -150,7 +150,7 @@ internal final class DefaultPresence: Presence {

// CHA-PR6b to CHA-PR6f
do {
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations(requestedByFeature: .presence)
try await roomLifecycleManager.waitToBeAbleToPerformPresenceOperations()
} catch {
logger.log(message: "Error waiting to be able to perform presence leave operation: \(error)", level: .error)
throw error
Expand Down
4 changes: 2 additions & 2 deletions Sources/AblyChat/ErrorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct ErrorInfo: Error, CustomStringConvertible {
case let .fromAblyCocoa(ablyCocoaError):
ablyCocoaError.code
case let .internalError(internalError):
internalError.codeAndStatusCode.code.rawValue
internalError.code.rawValue
case let .initializer(args):
args.code
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public struct ErrorInfo: Error, CustomStringConvertible {
case let .fromAblyCocoa(ablyCocoaError):
ablyCocoaError.statusCode
case let .internalError(internalError):
internalError.codeAndStatusCode.statusCode
internalError.code.statusCode
case let .initializer(args):
args.statusCode
}
Expand Down
Loading