Skip to content

Commit

Permalink
Fix error code when fetching room with inconsistent options
Browse files Browse the repository at this point in the history
I should have done this in e7612c9.

The 40000 error code does not have a name in the spec or in the JS Chat
SDK, so I took `badRequest` from errors.json [1].

[1] https://github.com/ably/ably-common/blob/main/protocol/errors.json
  • Loading branch information
lawrence-forooghian committed Dec 10, 2024
1 parent 76984aa commit 1756aa3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
16 changes: 7 additions & 9 deletions Sources/AblyChat/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public let errorDomain = "AblyChatErrorDomain"
The error codes for errors in the ``errorDomain`` error domain.
*/
public enum ErrorCode: Int {
/// ``Rooms.get(roomID:options:)`` was called with a different set of room options than was used on a previous call. You must first release the existing room instance using ``Rooms.release(roomID:)``.
///
/// TODO this code is a guess, revisit in https://github.com/ably-labs/ably-chat-swift/issues/32
case inconsistentRoomOptions = 1
/// The user attempted to perform an invalid action.
case badRequest = 40000

case messagesAttachmentFailed = 102_001
case presenceAttachmentFailed = 102_002
Expand All @@ -38,7 +36,7 @@ public enum ErrorCode: Int {

/// Has a case for each of the ``ErrorCode`` cases that imply a fixed status code.
internal enum CaseThatImpliesFixedStatusCode {
case inconsistentRoomOptions
case badRequest
case messagesAttachmentFailed
case presenceAttachmentFailed
case reactionsAttachmentFailed
Expand All @@ -56,8 +54,8 @@ public enum ErrorCode: Int {

internal var toNumericErrorCode: ErrorCode {
switch self {
case .inconsistentRoomOptions:
.inconsistentRoomOptions
case .badRequest:
.badRequest
case .messagesAttachmentFailed:
.messagesAttachmentFailed
case .presenceAttachmentFailed:
Expand Down Expand Up @@ -93,7 +91,7 @@ public enum ErrorCode: Int {
internal var statusCode: Int {
// These status codes are taken from the "Chat-specific Error Codes" section of the spec.
switch self {
case .inconsistentRoomOptions,
case .badRequest,
.roomInFailedState,
.roomIsReleasing,
.roomIsReleased,
Expand Down Expand Up @@ -175,7 +173,7 @@ internal enum ChatError {
internal var codeAndStatusCode: ErrorCodeAndStatusCode {
switch self {
case .inconsistentRoomOptions:
.fixedStatusCode(.inconsistentRoomOptions)
.fixedStatusCode(.badRequest)
case let .attachmentFailed(feature, _):
switch feature {
case .messages:
Expand Down
8 changes: 4 additions & 4 deletions Tests/AblyChatTests/DefaultRoomsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ struct DefaultRoomsTests {
_ = try await rooms.get(roomID: roomID, options: options)

// When: get(roomID:options:) is called with the same ID but different options
// Then: It throws an inconsistentRoomOptions error
// Then: It throws a `badRequest` error
let differentOptions = RoomOptions(presence: .init(subscribe: false))

await #expect {
try await rooms.get(roomID: roomID, options: differentOptions)
} throws: { error in
isChatError(error, withCodeAndStatusCode: .fixedStatusCode(.inconsistentRoomOptions))
isChatError(error, withCodeAndStatusCode: .fixedStatusCode(.badRequest))
}
}

Expand Down Expand Up @@ -183,13 +183,13 @@ struct DefaultRoomsTests {
_ = await operationWaitSubscription.first { $0.waitingOperationType == .get && $0.waitedOperationType == .release }

// When: get(roomID:options:) is called with the same ID but different options
// Then: The second call to get(roomID:options:) throws an inconsistentRoomOptions error
// Then: The second call to get(roomID:options:) throws a `badRequest` error
let differentOptions = RoomOptions(presence: .init(subscribe: false))

await #expect {
try await rooms.get(roomID: roomID, options: differentOptions)
} throws: { error in
isChatError(error, withCodeAndStatusCode: .fixedStatusCode(.inconsistentRoomOptions))
isChatError(error, withCodeAndStatusCode: .fixedStatusCode(.badRequest))
}

// Post-test: Allow the CHA-RC1g release operation to complete
Expand Down

0 comments on commit 1756aa3

Please sign in to comment.