Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-5144] Implement CHA-PR3h etc #163

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

lawrence-forooghian
Copy link
Collaborator

@lawrence-forooghian lawrence-forooghian commented Nov 28, 2024

Resolves #151. See commit messages for more details.

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced error handling with clearer categorization of error codes and status codes in the Ably Chat SDK.
  • Bug Fixes

    • Improved error messages for room status transitions and presence operations, ensuring more accurate reporting of issues.
  • Documentation

    • Updated documentation for error handling methods and functions to reflect changes and improve clarity.
  • Tests

    • Adjusted tests to validate new error handling logic, including status codes alongside error codes for more robust error reporting.

Copy link

coderabbitai bot commented Nov 28, 2024

Walkthrough

The pull request introduces significant changes to the error handling mechanisms within the Ably Chat SDK. Key modifications include updates to the DefaultMessages, Errors, RoomFeature, and RoomLifecycleManager files, enhancing error categorization and documentation. The ErrorCode enum has been refined, with new internal enums added for better status code management. Error handling in various methods has been updated to reflect these changes, ensuring clearer and more structured error reporting. Additionally, tests have been adjusted to validate the new error handling logic.

Changes

File Path Change Summary
Sources/AblyChat/DefaultMessages.swift Updated error handling in serialOnChannelAttach method; improved comments for clarity.
Sources/AblyChat/Errors.swift Removed nonspecific case; added new enums for error categorization; updated ChatError enum.
Sources/AblyChat/RoomFeature.swift Updated documentation for waitToBeAbleToPerformPresenceOperations to reflect new error handling.
Sources/AblyChat/RoomLifecycleManager.swift Modified error handling in waitToBeAbleToPerformPresenceOperations; updated comments for clarity.
Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift Enhanced error reporting in tests; updated methods to include status codes in assertions.
Tests/AblyChatTests/DefaultRoomsTests.swift Updated error handling in get_throwsErrorWhenOptionsDoNotMatch test to include status codes.
Tests/AblyChatTests/Helpers/Helpers.swift Changed isChatError function to accept ErrorCodeAndStatusCode type for improved error handling.

Assessment against linked issues

Objective Addressed Explanation
Throw RoomInInvalidState instead of 40000 ( #151, ECO-5144)

Poem

In the code where errors dwell,
Changes made to serve us well.
With clearer paths and status bright,
Our chat flows smooth, a pure delight! 🐇✨
Error codes now dance in line,
In Ably's world, all will be fine!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (6)
Sources/AblyChat/DefaultMessages.swift (1)

237-244: Consider adding error code documentation

The error message "Channel failed to attach" could be more descriptive about the reason for failure and potential remediation steps.

Consider enhancing the error message:

- message: "Channel failed to attach"
+ message: "Channel failed to attach. This may occur due to network issues or invalid channel state. Please check network connectivity and retry."
Sources/AblyChat/RoomLifecycleManager.swift (1)

1234-1235: LGTM: Error handling matches specifications

The error handling correctly implements the referenced specifications (CHA-PR3h, CHA-PR10h, CHA-PR6h, CHA-T2g) by throwing .presenceOperationRequiresRoomAttach with the requesting feature.

Consider grouping the specification references in the comment for better readability:

-            // CHA-PR3h, CHA-PR10h, CHA-PR6h, CHA-T2g
+            // Presence operation requirements:
+            // - CHA-PR3h: Enter
+            // - CHA-PR10h: Update
+            // - CHA-PR6h: Leave
+            // - CHA-T2g: Get
Sources/AblyChat/Errors.swift (4)

113-123: Unify CaseThatImpliesVariableStatusCode with ErrorCode

The CaseThatImpliesVariableStatusCode enum contains a single case that mirrors the ErrorCode enum. To simplify the code structure, consider integrating variable status codes directly into the ErrorCode enum using associated values or a mapping mechanism.

For example:

public enum ErrorCode: Int {
    // Existing cases...
    case roomInInvalidState = 102_107
    // ...

    /// Function to get the status code for variable status code errors.
    func statusCode(forError error: ChatError) -> Int {
        switch error {
        case .roomTransitionedToInvalidStateForPresenceOperation:
            return 500
        case .presenceOperationRequiresRoomAttach:
            return 400
        default:
            return self.fixedStatusCode ?? 0 // Use the fixed status code if available
        }
    }
}

This approach centralizes the error code and status code handling within the ErrorCode enum.


128-148: Simplify ErrorCodeAndStatusCode by leveraging associated values

The ErrorCodeAndStatusCode enum adds complexity by wrapping error codes with status codes. To streamline error handling, consider using associated values within the ErrorCode enum to represent variable status codes, which can simplify the codebase and make error management more intuitive.

Example modification:

public enum ErrorCode: Int {
    // Existing cases...

    case roomInInvalidState(statusCode: Int)

    // ...

    /// Function to get the status code.
    var statusCode: Int {
        switch self {
        case .roomInInvalidState(let statusCode):
            return statusCode
        default:
            return self.fixedStatusCode ?? 0
        }
    }
}

This change allows you to store the status code directly with the error code, eliminating the need for the separate ErrorCodeAndStatusCode enum.


265-265: Enhance error message for roomTransitionedToInvalidStateForPresenceOperation

The current error message is generic and may not provide sufficient context to the developer or user. Consider including specific details about the invalid state or providing guidance on how to rectify the issue.

For example:

case .roomTransitionedToInvalidStateForPresenceOperation:
    "The room operation failed because the room has transitioned to a state that does not allow presence operations. Please ensure the room is in a valid state before retrying."

Line range hint 293-303: Avoid reliance on private ARTErrorInfo implementation details

The code uses hardcoded keys like "ARTErrorInfoStatusCode" and comments indicate copying from private implementations in ably-cocoa. This approach can lead to fragile code if the underlying library changes. It's recommended to:

  1. Request public APIs: Reach out to the ably-cocoa maintainers to expose necessary properties or methods needed for proper error handling.
  2. Encapsulate error construction: Create a wrapper or utility that safely constructs ARTErrorInfo without depending on internal implementations.

By reducing dependency on private APIs, you ensure long-term stability and compatibility of your SDK.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 86dfb3b and 7a0952e.

📒 Files selected for processing (7)
  • Sources/AblyChat/DefaultMessages.swift (1 hunks)
  • Sources/AblyChat/Errors.swift (6 hunks)
  • Sources/AblyChat/RoomFeature.swift (1 hunks)
  • Sources/AblyChat/RoomLifecycleManager.swift (1 hunks)
  • Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (13 hunks)
  • Tests/AblyChatTests/DefaultRoomsTests.swift (1 hunks)
  • Tests/AblyChatTests/Helpers/Helpers.swift (1 hunks)
🔇 Additional comments (13)
Tests/AblyChatTests/Helpers/Helpers.swift (2)

7-7: LGTM! Improved error handling structure

The function signature change from withCode to withCodeAndStatusCode better reflects the structured approach to error handling, aligning with the PR's objective to improve error state management.


13-14: LGTM! Verify test coverage for the new error validation

The error validation logic has been properly updated to handle both error codes and status codes separately through the new ErrorCodeAndStatusCode type.

Let's verify the test coverage for this new error validation structure:

✅ Verification successful

Based on the search results, I can generate the final response as we have sufficient information about the test coverage of the new error validation structure.

Test coverage for the new error validation is comprehensive and well-structured

The codebase shows thorough test coverage for the new error validation structure with ErrorCodeAndStatusCode:

  • Multiple test cases in DefaultRoomsTests.swift and DefaultRoomLifecycleManagerTests.swift validate various error scenarios
  • Tests cover both fixed status codes (e.g., .roomIsReleasing, .roomIsReleased, .roomInFailedState) and variable status codes
  • Error validation includes checking for specific error causes and custom messages
  • Test helper isChatError properly validates both the error code and status code components
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for test cases using the new error validation structure

# Search for test cases using isChatError with the new parameter structure
rg -A 5 "isChatError.*withCodeAndStatusCode" "Tests/"

# Search for the definition and usage of ErrorCodeAndStatusCode to ensure proper test coverage
ast-grep --pattern 'enum ErrorCodeAndStatusCode {
  $$$
}'

Length of output: 8176

Sources/AblyChat/RoomFeature.swift (2)

44-48: Documentation updates look good, verifying implementation alignment.

The documentation changes clearly describe the error handling scenarios and align well with the PR objectives. The updates properly reflect the transition from error code 40000 to specific error types.

Let's verify that the implementation matches the documented behavior:

✅ Verification successful

Let me gather more information about the implementation of waitToBeAbleToPerformPresenceOperations to verify it fully matches the documented behavior.


Implementation correctly matches documented behavior

The implementation in RoomLifecycleManager.swift fully aligns with the documented error handling scenarios:

  1. For ATTACHING status (CHA-RL9): The code waits for the next room status change and throws roomTransitionedToInvalidStateForPresenceOperation if the new status is not ATTACHED.
  2. For ATTACHED status (CHA-PR3e): The code returns immediately as documented.
  3. For other statuses (CHA-PR3h): The code throws presenceOperationRequiresRoomAttach with status code 400.

The error types are properly defined in Errors.swift with the correct status codes and messages.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify error handling implementation matches documentation
# Search for the error handling implementation in the codebase

# Check for roomTransitionedToInvalidStateForPresenceOperation implementation
rg -t swift "roomTransitionedToInvalidStateForPresenceOperation" -A 5

# Check for presenceOperationRequiresRoomAttach implementation
rg -t swift "presenceOperationRequiresRoomAttach" -A 5

# Check for waitToBeAbleToPerformPresenceOperations implementation
ast-grep --pattern 'func waitToBeAbleToPerformPresenceOperations'

Length of output: 6535


Script:

#!/bin/bash
# Search for the implementation of waitToBeAbleToPerformPresenceOperations
rg -t swift "func waitToBeAbleToPerformPresenceOperations.*\{" -A 20

# Also check for any switch statements handling room states
rg -t swift "switch.*status.*\{" -A 15 Sources/AblyChat/RoomLifecycleManager.swift

Length of output: 16350


Line range hint 63-65: Verify error propagation in DefaultFeatureChannel.

The implementation correctly delegates to roomLifecycleManager, maintaining good separation of concerns. However, we should verify that errors are properly propagated through the chain.

Let's verify the error propagation chain:

✅ Verification successful

Let me gather more information about the error handling implementation.


Based on the comprehensive analysis of the error handling implementation, I can now provide the final response:

Error propagation is properly implemented in the waitToBeAbleToPerformPresenceOperations chain

The implementation correctly propagates errors through the following chain:

  • DefaultFeatureChannel delegates to RoomLifecycleManager
  • RoomLifecycleManager implements proper error handling for different states:
    • Throws ARTErrorInfo with ChatError.presenceOperationRequiresRoomAttach when not in correct state
    • Propagates errors during state transitions with proper error context
    • Handles attachment failures with appropriate error wrapping

The error handling follows the documented behavior in the function comments and is well-tested as evidenced by the comprehensive test suite in DefaultRoomLifecycleManagerTests.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify error propagation through the call chain

# Check RoomLifecycleManager implementation
ast-grep --pattern 'func waitToBeAbleToPerformPresenceOperations(requestedByFeature $$$) {
  $$$
}'

# Check for error handling tests
rg -t swift "waitToBeAbleToPerformPresenceOperations.*throws" -A 5 --glob "*Tests.swift"

Length of output: 3015


Script:

#!/bin/bash
# Check the implementation of waitToBeAbleToPerformPresenceOperations in RoomLifecycleManager
rg -t swift "waitToBeAbleToPerformPresenceOperations.*throws" -B 5 -A 15

# Check for error types and handling
rg -t swift "ARTErrorInfo" -A 3

Length of output: 82093

Tests/AblyChatTests/DefaultRoomsTests.swift (1)

81-81: LGTM! Error assertion update aligns with new error handling structure.

The change from withCode to withCodeAndStatusCode properly validates both the error code and status code, providing more comprehensive error checking. This aligns with the PR's objective of improving error handling mechanisms.

Let's verify the consistency of error handling across the test suite:

✅ Verification successful

Consider updating remaining withCode assertions to use withCodeAndStatusCode for consistency

The verification reveals inconsistent error assertion patterns across test files:

  • DefaultRoomLifecycleManagerTests.swift uses both old .create(withCode:) and new withCodeAndStatusCode patterns
  • DefaultMessagesTests.swift and ChatAPITests.swift still use the old .create(withCode:) pattern

Let's verify if these are intentionally different or need updating:


The old-style error assertions are used correctly and don't require updates

After analyzing the context:

  1. In DefaultMessagesTests.swift, the old-style assertion is used to test a specific Ably Realtime error (ARTErrorInfo) with status code, which is correct.
  2. In ChatAPITests.swift, it's used to mock an internal server error from Ably's SDK.
  3. In DefaultRoomLifecycleManagerTests.swift, it's used for creating mock errors in test setup, not for asserting Chat SDK-specific errors.

The new withCodeAndStatusCode pattern is correctly used for Chat SDK-specific error assertions, while the old withCode pattern is appropriately used for Ably Realtime SDK errors and mocks. This separation is intentional and correct.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining old-style error assertions that might need updating

# Search for old-style error assertions
rg "withCode:" "Tests/"

# Search for new-style error assertions for comparison
rg "withCodeAndStatusCode:" "Tests/"

Length of output: 3193


Script:

#!/bin/bash
# Check the context of old-style assertions to understand their usage
rg -B 2 -A 2 "withCode:" Tests/AblyChatTests/DefaultMessagesTests.swift Tests/AblyChatTests/ChatAPITests.swift Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift

Length of output: 3179

Sources/AblyChat/DefaultMessages.swift (2)

237-241: Verify error code mapping

The change introduces a new error code case messagesAttachmentFailed. We should verify that this maps to the correct error code and status code as per the API specifications.

✅ Verification successful

Error code mapping is correctly implemented

The verification confirms that:

  • messagesAttachmentFailed is defined with error code 102001 in the ErrorCode enum
  • It's properly mapped as a fixed status code case
  • The error code and status code are correctly propagated through the error handling system
  • The implementation is consistent with test cases that verify this behavior
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for the error code definition and its usage
ast-grep --pattern 'enum ErrorCode {
  $$$
  case messagesAttachmentFailed
  $$$
}'

Length of output: 85


Script:

#!/bin/bash
# Search for the error code definition and its usage with a broader pattern
rg -A 10 "enum ErrorCode" --type swift

Length of output: 1792


Script:

#!/bin/bash
# Search for messagesAttachmentFailed usage
rg "messagesAttachmentFailed" --type swift

Length of output: 1034


Script:

#!/bin/bash
# Search for CaseThatImpliesFixedStatusCode
rg "CaseThatImpliesFixedStatusCode" --type swift -A 5

Length of output: 1602


236-236: Consider addressing error handling TODO

There's a TODO comment about revisiting error handling (issue #32). Given that this PR implements significant error handling changes, it might be worth addressing this TODO now to ensure consistency.

Let's check the referenced issue:

Sources/AblyChat/RoomLifecycleManager.swift (1)

1228-1228: LGTM: Improved error handling clarity

The change from error code 40000 to .roomTransitionedToInvalidStateForPresenceOperation provides better error context by including the cause from the room status change.

Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (3)

148-148: LGTM: Consistent error code verification for room state errors

The changes correctly implement the standardized error verification using fixedStatusCode for room state errors (roomIsReleasing and roomIsReleased). This aligns with the PR objective to improve error handling.

Also applies to: 163-163


376-376: LGTM: Comprehensive error code verification updates

The changes systematically update error verification across multiple test cases to use fixedStatusCode for:

  • Message attachment failures
  • Room state errors (releasing, released, failed)
  • Presence detachment failures

This provides consistent error handling verification throughout the test suite.

Also applies to: 457-457, 564-564, 579-579, 598-598, 697-697, 1098-1098


Line range hint 2090-2099: LGTM: Proper implementation of RoomInInvalidState error handling

The changes correctly implement the new RoomInInvalidState error handling with:

  • Proper error cause propagation
  • Appropriate status codes (500 for transitions, 400 for initial state)
  • Clear error messages
  • Comprehensive spec documentation

This fulfills the PR objective to replace error code 40000 with RoomInInvalidState.

Also applies to: 2118-2121, 2124-2126, 2139-2140

Sources/AblyChat/Errors.swift (2)

205-209: Ensure consistent handling of variable status codes

The status codes assigned to .roomTransitionedToInvalidStateForPresenceOperation (500) and .presenceOperationRequiresRoomAttach (400) are hardcoded but categorized under variableStatusCode. Verify whether these status codes are indeed variable or should be fixed. If they are fixed, consider moving them to the fixed status code category for clarity and consistency.

If the status codes are expected to vary based on context, ensure that the mechanisms to assign dynamic status codes are correctly implemented.


277-277: Confirm proper propagation of underlying error causes

When handling .roomTransitionedToInvalidStateForPresenceOperation, ensure that the optional cause is being correctly propagated and logged. This will aid in debugging by providing the root cause of the error when available.

Double-check that any wrapped ARTErrorInfo instances are included in the error reporting mechanisms and are accessible to the developers.

Sources/AblyChat/DefaultMessages.swift Show resolved Hide resolved
Sources/AblyChat/Errors.swift Show resolved Hide resolved
@lawrence-forooghian
Copy link
Collaborator Author

CI isn't running because Actions have become disabled after we migrated this repo out of ably-labs. But will put into review anyway.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
Sources/AblyChat/Errors.swift (1)

112-122: Add documentation explaining variable status codes

Consider adding documentation to explain when and why certain error codes have variable status codes versus fixed ones. This would help maintainers understand when to use each type.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7a0952e and 951e66b.

📒 Files selected for processing (4)
  • Sources/AblyChat/Errors.swift (6 hunks)
  • Sources/AblyChat/RoomFeature.swift (1 hunks)
  • Sources/AblyChat/RoomLifecycleManager.swift (1 hunks)
  • Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (13 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • Sources/AblyChat/RoomFeature.swift
  • Sources/AblyChat/RoomLifecycleManager.swift
🔇 Additional comments (15)
Sources/AblyChat/Errors.swift (4)

125-150: LGTM! Well-structured error code and status code handling

The implementation provides a clean separation between fixed and variable status codes while maintaining type safety.


166-209: LGTM! Successfully implements CHA-PR3h requirements

The changes correctly implement the requirement to throw RoomInInvalidState with appropriate status codes (400 for presence operation requiring room attach, 500 for room transition to invalid state). The spec references in comments provide good traceability.


37-110: 🛠️ Refactor suggestion

Consider refactoring to reduce code duplication

The CaseThatImpliesFixedStatusCode enum duplicates cases from ErrorCode, which could lead to maintenance challenges.

The previous review comment suggesting refactoring to eliminate redundancy using a computed property in ErrorCode is still applicable.


Line range hint 293-303: Consider addressing TODOs in a follow-up PR

The TODOs reference implementation details that should be revisited in issue #32, particularly around:

  1. The copying of ARTErrorInfo creation logic
  2. Documentation of NSUnderlyingErrorKey usage in ably-cocoa
Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (11)

148-148: LGTM! Error assertion updated for room releasing state.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


163-163: LGTM! Error assertion updated for room released state.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


376-376: LGTM! Error assertion updated for messages attachment failure.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


457-457: LGTM! Error assertion updated for messages attachment failure.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


564-564: LGTM! Error assertion updated for room releasing state.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


579-579: LGTM! Error assertion updated for room released state.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


598-598: LGTM! Error assertion updated for room in failed state.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


697-697: LGTM! Error assertion updated for presence detachment failure.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


1098-1098: LGTM! Error assertion updated for presence detachment failure.

The change from withCode to withCodeAndStatusCode aligns with the PR objective of improving error handling.


Line range hint 2090-2099: LGTM! Error assertion updated with specific status code for room in invalid state.

The change implements the new error handling with status code 500 for invalid room state during presence operations, aligning with the PR objective.


Line range hint 2118-2140: LGTM! Error assertion updated with specific status code and message for room in invalid state.

The change implements the new error handling with:

  • Status code 400 for invalid room state
  • Clear error message explaining that the room must be attached first
  • Proper test coverage for the error scenario

This aligns perfectly with the PR objective of improving error handling clarity.

Copy link
Collaborator

@maratal maratal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

This is in preparation for implementing CHA-PR3h, in which a given
numerical error code no longer necessarily implies a specific status
code (specifically, RoomInInvalidState may now have a 400 or 500 status
code depending on the context in which it is thrown).

The internal API introduced here is a bit convoluted and verbose, but
I’ve done it this way for two reasons:

1. It reflects the way the spec is written; most of the time a numeric
   code has a status code written alongside it

2. For the numerical error codes that _do_ always imply a certain status
   code, I want to be able to get that status code still, so that the
   existing Messages code that throws an error with the
   messagesAttachmentFailed code, as well as the isChatError(…) test
   helper, continue to work as they currently do.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
Sources/AblyChat/Errors.swift (1)

Line range hint 293-303: Consider improving documentation for ARTErrorInfo initialization.

While the implementation is correct, the TODO comments suggest documentation improvements needed in ably-cocoa. Consider:

  • Documenting the NSUnderlyingErrorKey usage
  • Clarifying the ARTErrorInfoStatusCode implementation details
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 951e66b and cf769a1.

📒 Files selected for processing (7)
  • Sources/AblyChat/DefaultMessages.swift (1 hunks)
  • Sources/AblyChat/Errors.swift (6 hunks)
  • Sources/AblyChat/RoomFeature.swift (1 hunks)
  • Sources/AblyChat/RoomLifecycleManager.swift (1 hunks)
  • Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (13 hunks)
  • Tests/AblyChatTests/DefaultRoomsTests.swift (1 hunks)
  • Tests/AblyChatTests/Helpers/Helpers.swift (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • Tests/AblyChatTests/DefaultRoomsTests.swift
  • Sources/AblyChat/DefaultMessages.swift
  • Sources/AblyChat/RoomLifecycleManager.swift
  • Tests/AblyChatTests/Helpers/Helpers.swift
  • Sources/AblyChat/RoomFeature.swift
🔇 Additional comments (6)
Sources/AblyChat/Errors.swift (3)

37-122: Well-structured error code classification!

The separation of error codes into fixed and variable status codes improves error handling clarity and maintainability. The implementation is thorough with:

  • Clear documentation of each enum's purpose
  • Exhaustive case handling
  • Type-safe conversion to numeric error codes

125-150: Well-designed error code and status code pairing!

The ErrorCodeAndStatusCode enum provides a type-safe way to handle error codes with their corresponding status codes. The implementation:

  • Clearly separates fixed and variable status code cases
  • Provides convenient access to both code and status code
  • Maintains type safety throughout

204-209: Successfully implemented CHA-PR3h requirements!

The changes correctly implement the new error handling requirements by:

  • Adding proper status code handling for invalid room states (500 for transitions, 400 for presence operations)
  • Including clear spec references in comments (CHA-RL9c, CHA-PR3h, etc.)
Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift (3)

Line range hint 148-163: Comprehensive error assertion updates!

The test assertions have been properly updated to verify both error codes and status codes using the new withCodeAndStatusCode method. This ensures proper validation of the new error handling structure.


Line range hint 2090-2099: Thorough testing of presence operation error handling!

The test properly verifies the new error handling requirements:

  • Checks for RoomInInvalidState error with status code 500
  • Verifies the error cause matches the room status change
  • Validates the complete error chain

Line range hint 2118-2140: Well-documented and implemented presence operation tests!

The test implementation:

  • Properly references relevant specs (CHA-PR3h, CHA-PR10h, etc.)
  • Verifies correct error message for room attachment requirement
  • Validates the expected status code 400 for invalid states

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

wait-to-be-able-to-presence: throw RoomInInvalidState instead of 40000
2 participants