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

Unify error throwing methods #878

Merged
merged 11 commits into from
Aug 7, 2024

Conversation

gwbaik9717
Copy link
Contributor

@gwbaik9717 gwbaik9717 commented Jul 28, 2024

What this PR does / why we need it?

This PR unifies error throwing methods by refactoring the logger to focus solely on logging without throwing errors, and use YorkieError to throw errors.

Any background context you want to provide?

The current implementation of the logger includes both logging and error throwing, which can lead to confusion and type problems. By separating these concerns, we can ensure that the logger is only responsible for logging messages, while errors are thrown explicitly using YorkieError.

What are the relevant tickets?

Fixes #696

Checklist

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Enhanced error handling across various classes and modules, implementing structured error reporting with YorkieError and specific error codes.
  • Bug Fixes

    • Improved clarity of error messages, ensuring that specific errors are thrown instead of generic ones, aiding in debugging and user feedback.

This commit separates the error throwing and logging functionalities in logger.fatal. The logger now solely handles logging messages, while errors are thrown independently using YorkieError.
This commit standardizes the naming conventions for errors starting with 'Err'
This commit replaces all instances of throw new Error with YorkieError to provide more specific and consistent error handling throughout the codebase.
Copy link

coderabbitai bot commented Jul 28, 2024

Walkthrough

The recent updates to the Yorkie SDK significantly enhance error handling across various components by implementing structured error reporting through the YorkieError class. This transition replaces vague error messages with specific codes and descriptions, fostering greater clarity and maintainability. The unified approach ensures consistent error management throughout the codebase, empowering developers to diagnose issues more effectively and respond to exceptions with confidence.

Changes

Files Change Summary
src/api/converter.ts Improved error handling with YorkieError for invalid types and arguments.
src/document/crdt/element_rht.ts Enhanced error handling by throwing YorkieError when nodes are not found.
src/document/crdt/rga_tree_list.ts, src/document/crdt/rga_tree_split.ts, src/document/json/counter.ts, src/document/operation/*.ts Replaced generic Error with YorkieError for improved error specificity.
src/util/observable.ts, src/util/splay_tree.ts Introduced structured error handling, improving clarity in error management.
test/integration/primitive_test.ts Updated tests to utilize YorkieError for enhanced error specificity.

Assessment against linked issues

Objective Addressed Explanation
Unify Error Throwing Methods (#696)
Standardize error handling approach
Improve clarity and maintainability of error codes
Enhance error reporting in tests

Poem

In a garden of code where bunnies hop,
Errors once tangled now flow like a mop.
With YorkieError shining bright,
Making debugging a pure delight.
Hooray for clarity, hooray for the cheer,
Our errors are friendly, come lend us an ear! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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: 25

Outside diff range, codebase verification and nitpick comments (1)
src/document/document.ts (1)

1881-1884: Ensure proper error handling for redo function calls

The search results indicate that the redo function throws YorkieError, but there is no evidence of try-catch blocks or explicit error handling around calls to redo() in the implementation files. This could lead to unhandled exceptions in the application.

  • Ensure that all calls to redo() are wrapped in try-catch blocks or have appropriate error handling to manage YorkieError.
Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to redo handle the new YorkieError appropriately.


The redo function is used extensively in the codebase, particularly in test files, which is a good indication that error handling is being tested. However, we need to ensure that all instances where redo is used in the actual implementation (not just tests) handle the YorkieError appropriately. Let's search for redo calls in implementation files and check if they are within try-catch blocks or have appropriate error handling.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `redo` handle the new `YorkieError` appropriately.

# Test: Search for the function usage. Expect: Appropriate handling of `YorkieError`.
rg --type ts -A 5 $'redo'

Length of output: 26840


Script:

#!/bin/bash
# Search for redo calls in implementation files and check for error handling.
rg --type ts 'redo()' -A 5 | grep -E 'try|catch|YorkieError'

Length of output: 235

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 83a6369 and 35be776.

Files selected for processing (28)
  • src/api/converter.ts (6 hunks)
  • src/document/crdt/element_rht.ts (3 hunks)
  • src/document/crdt/rga_tree_list.ts (4 hunks)
  • src/document/crdt/rga_tree_split.ts (4 hunks)
  • src/document/crdt/root.ts (2 hunks)
  • src/document/crdt/tree.ts (5 hunks)
  • src/document/document.ts (5 hunks)
  • src/document/json/counter.ts (3 hunks)
  • src/document/json/text.ts (11 hunks)
  • src/document/json/tree.ts (24 hunks)
  • src/document/operation/add_operation.ts (2 hunks)
  • src/document/operation/edit_operation.ts (2 hunks)
  • src/document/operation/increase_operation.ts (2 hunks)
  • src/document/operation/move_operation.ts (2 hunks)
  • src/document/operation/operation.ts (2 hunks)
  • src/document/operation/remove_operation.ts (2 hunks)
  • src/document/operation/set_operation.ts (2 hunks)
  • src/document/operation/style_operation.ts (2 hunks)
  • src/document/operation/tree_edit_operation.ts (2 hunks)
  • src/document/operation/tree_style_operation.ts (2 hunks)
  • src/util/error.ts (3 hunks)
  • src/util/index_tree.ts (22 hunks)
  • src/util/logger.ts (1 hunks)
  • src/util/observable.ts (2 hunks)
  • src/util/splay_tree.ts (2 hunks)
  • test/integration/client_test.ts (3 hunks)
  • test/integration/object_test.ts (2 hunks)
  • test/integration/primitive_test.ts (2 hunks)
Additional context used
Biome
test/integration/primitive_test.ts

[error] 41-41: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

GitHub Check: build (18.x)
src/document/crdt/element_rht.ts

[failure] 118-118:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 148-148:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase

src/document/crdt/root.ts

[failure] 138-138:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase

src/document/crdt/rga_tree_list.ts

[failure] 184-184:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 238-238:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 245-245:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 294-294:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase

src/document/crdt/rga_tree_split.ts

[failure] 638-638:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 797-797:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


[failure] 851-851:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase

Additional comments not posted (79)
test/integration/primitive_test.ts (1)

6-6: New import statement is appropriate.

The addition of Code and YorkieError is necessary for the new error handling approach.

src/util/error.ts (2)

30-31: New error codes improve error handling.

The addition of ErrInvalidType, ErrDummy, ErrOperationNotReady, and ErrOperationNotPermitted enhances the expressiveness of error handling.

Also applies to: 33-34, 51-52, 54-55


67-67: Simplified toString method improves clarity.

The updated toString method focuses on the error code and message, reducing verbosity.

src/util/logger.ts (1)

Line range hint 55-55:
Removing error throwing from fatal method is appropriate.

The removal aligns with the goal of separating logging and error throwing responsibilities.

src/document/json/counter.ts (3)

28-28: Importing Code and YorkieError.

The import statement for Code and YorkieError is appropriate for the new error handling approach.


82-84: Improved error handling in increase method.

The increase method now throws a YorkieError with a specific error code and message when the counter is not initialized. This enhances clarity and consistency in error handling.


109-109: Improved error handling in toJSForTest method.

The toJSForTest method now throws a YorkieError with a specific error code and message when the counter is not ready. This improves the robustness and clarity of error handling.

src/document/operation/add_operation.ts (3)

26-26: Importing Code and YorkieError.

The import statement for Code and YorkieError is appropriate for the new error handling approach.


64-66: Improved error handling when parent object is not found.

The execute method now throws a YorkieError with a specific error code and message when the parent object is not found. This enhances clarity and consistency in error handling.


69-71: Improved error handling for invalid parent object type.

The execute method now throws a YorkieError with a specific error code and message when the parent object is not an instance of CRDTArray. This improves the robustness and clarity of error handling.

src/document/operation/move_operation.ts (3)

25-25: Importing Code and YorkieError.

The import statement for Code and YorkieError is appropriate for the new error handling approach.


68-70: Improved error handling when parent object is not found.

The execute method now throws a YorkieError with a specific error code and message when the parent object is not found. This enhances clarity and consistency in error handling.


73-75: Improved error handling for invalid parent object type.

The execute method now throws a YorkieError with a specific error code and message when the parent object is not an instance of CRDTArray. This improves the robustness and clarity of error handling.

src/document/operation/increase_operation.ts (2)

66-68: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.


71-73: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.

src/document/operation/remove_operation.ts (2)

72-74: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.


77-79: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.

src/document/operation/style_operation.ts (2)

81-83: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.


86-88: Good practice: Centralized error message and structured error handling

Centralizing the error message definition and throwing a YorkieError with a specific error code improves readability and maintainability.

src/document/operation/set_operation.ts (3)

28-28: Import statement for error handling approved.

The inclusion of Code and YorkieError is necessary for the new error handling mechanism.


70-72: Improved error handling for missing object approved.

The code now logs a fatal message and throws a YorkieError when an object is not found, providing a structured error response.


75-77: Improved error handling for invalid parent object approved.

The code now logs a fatal message and throws a YorkieError when the parent object is not an instance of CRDTObject, providing a structured error response.

src/document/operation/edit_operation.ts (3)

28-28: Import statement for error handling approved.

The inclusion of Code and YorkieError is necessary for the new error handling mechanism.


87-89: Improved error handling for missing parent object approved.

The code now logs a fatal message and throws a YorkieError when a parent object is not found, providing a structured error response.


92-94: Improved error handling for invalid parent object approved.

The code now logs a fatal message and throws a YorkieError when the parent object is not an instance of CRDTText, providing a structured error response.

src/util/observable.ts (3)

19-19: Import statement for error handling approved.

The inclusion of Code and YorkieError is necessary for the new error handling mechanism.


110-112: Improved error handling for missing observer approved.

The code now logs a fatal message and throws a YorkieError when the nextOrObserver parameter is missing, providing a structured error response.


116-118: Improved error handling for finalized observable approved.

The code now logs a fatal message and throws a YorkieError when the observable has been finalized due to a prior error, providing a structured error response.

src/document/crdt/element_rht.ts (2)

118-120: Improved error handling.

The change improves error handling by throwing a YorkieError with a specific error code.

Tools
GitHub Check: build (18.x)

[failure] 118-118:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase


148-152: Improved error handling.

The change improves error handling by throwing a YorkieError with a specific error code.

Tools
GitHub Check: build (18.x)

[failure] 148-148:
Variable name ERROR_MESSAGE must match one of the following formats: camelCase, PascalCase

src/document/operation/tree_edit_operation.ts (1)

89-96: Improved error handling.

The change improves error handling by throwing a YorkieError with a specific error code.

src/document/operation/operation.ts (1)

214-217: Improved error handling.

The change improves error handling by throwing a YorkieError with a specific error code.

src/document/json/text.ts (2)

306-309: LGTM!

The changes improve error handling by throwing specific errors.


321-324: LGTM!

The changes improve error handling by throwing specific errors.

src/document/crdt/rga_tree_list.ts (1)

25-25: Import statement approved.

The import of Code and YorkieError is necessary for the new error handling approach.

src/util/splay_tree.ts (1)

18-18: Import statement approved.

The import of Code and YorkieError is necessary for the new error handling approach.

src/document/json/tree.ts (12)

46-46: Import statement approved.

The import of Code and YorkieError is necessary for the new error handling approach.


157-160: Error handling approved.

The use of YorkieError in validateTextNode improves error specificity.


179-182: Error handling approved.

The use of YorkieError in validateTreeNodes improves error specificity.


190-193: Error handling approved.

The use of YorkieError in validateTreeNodes improves error specificity.


260-263: Error handling approved.

The use of YorkieError in getSize improves error specificity.


274-277: Error handling approved.

The use of YorkieError in getNodeSize improves error specificity.


288-291: Error handling approved.

The use of YorkieError in getIndexTree improves error specificity.


302-305: Error handling approved.

The use of YorkieError in styleByPath improves error specificity.


309-312: Error handling approved.

The use of YorkieError in styleByPath improves error specificity.


345-348: Error handling approved.

The use of YorkieError in style improves error specificity.


352-355: Error handling approved.

The use of YorkieError in style improves error specificity.


394-397: Error handling approved.

The use of YorkieError in removeStyle improves error specificity.

src/document/crdt/rga_tree_split.ts (1)

29-29: Import statement for Code and YorkieError looks good.

The import statement is necessary for the new error handling approach.

src/util/index_tree.ts (10)

18-18: Import statement for Code and YorkieError looks good.

The import statement is necessary for the new error handling approach.


133-136: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


310-313: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


329-332: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


346-349: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


354-354: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code for the child not found case.


366-369: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


374-374: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code for the child not found case.


386-389: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.


401-404: Approve the use of YorkieError for better error handling.

The change improves error handling by using YorkieError with a specific error code.

test/integration/client_test.ts (3)

35-35: Import statement for YorkieError looks good.

The import statement is necessary for the new error handling approach.


136-136: Approve the use of YorkieError for better error handling in tests.

The change improves error handling in tests by using YorkieError with a specific error code.


214-214: Approve the use of YorkieError for better error handling in tests.

The change improves error handling in tests by using YorkieError with a specific error code.

test/integration/object_test.ts (2)

11-11: LGTM! Import statement is correct.

The addition of Code from @yorkie-js-sdk/src/util/error is necessary for the new error handling approach.


66-66: LGTM! Improved error handling.

The use of YorkieError with a specific error code enhances error specificity and aligns with the new error handling strategy.

src/document/crdt/tree.ts (5)

46-46: LGTM! Import statement is correct.

The addition of Code from @yorkie-js-sdk/src/util/error is necessary for the new error handling approach.


227-229: LGTM! Improved error handling.

The use of YorkieError with a specific error code enhances error specificity and aligns with the new error handling strategy.


519-522: LGTM! Improved error handling.

The use of YorkieError with a specific error code enhances error specificity and aligns with the new error handling strategy.


533-536: LGTM! Improved error handling.

The use of YorkieError with a specific error code enhances error specificity and aligns with the new error handling strategy.


1228-1231: LGTM! Improved error handling.

The use of YorkieError with a specific error code enhances error specificity and aligns with the new error handling strategy.

src/api/converter.ts (5)

205-208: LGTM! Verify the usage of the new error code.

The changes improve error specificity by using YorkieError with a specific error code Code.ErrInvalidType.

However, ensure that the new error code Code.ErrInvalidType is correctly used and handled throughout the codebase.

Verification successful

The usage of the error code Code.ErrInvalidType is consistent and appropriate.

The error code is used in both src/api/converter.ts and src/document/crdt/tree.ts to ensure type safety, aligning with the intended purpose.

  • src/api/converter.ts: Used to handle unsupported types during conversion.
  • src/document/crdt/tree.ts: Used to ensure the node type is correct before accessing or setting its value.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new error code `Code.ErrInvalidType`.

# Test: Search for the error code usage. Expect: Consistent handling of the new error code.
rg --type ts 'Code.ErrInvalidType'

Length of output: 340


Script:

#!/bin/bash
# Description: Gather context for the usage of `Code.ErrInvalidType` in `src/document/crdt/tree.ts`.

# Search for the lines around the usage of `Code.ErrInvalidType` to understand the context.
rg --type ts -A 5 -B 5 'Code.ErrInvalidType' src/document/crdt/tree.ts

Length of output: 568


222-225: LGTM! Verify the usage of the new error code.

The changes improve error specificity by using YorkieError with a specific error code Code.ErrInvalidType.

However, ensure that the new error code Code.ErrInvalidType is correctly used and handled throughout the codebase.

Verification successful

Let's inspect the usage of Code.ErrInvalidType in src/document/crdt/tree.ts to ensure consistent error handling.


The usage of the new error code Code.ErrInvalidType is consistent and appropriate throughout the codebase.

  • src/api/converter.ts: The error code is used with a clear message indicating an unsupported type.
  • src/document/crdt/tree.ts: The error code is used with messages indicating issues with element node values.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new error code `Code.ErrInvalidType`.

# Test: Search for the error code usage. Expect: Consistent handling of the new error code.
rg --type ts 'Code.ErrInvalidType'

Length of output: 340


Script:

#!/bin/bash
# Search for the context around the usage of Code.ErrInvalidType in src/document/crdt/tree.ts
rg --type ts -A 5 'Code.ErrInvalidType' src/document/crdt/tree.ts

Length of output: 319


Line range hint 201-204:
LGTM! Verify the usage of the new error code.

The changes improve error specificity by using YorkieError with a specific error code Code.ErrUnimplemented.

However, ensure that the new error code Code.ErrUnimplemented is correctly used and handled throughout the codebase.

Verification successful

The usage of the new error code Code.ErrUnimplemented is consistent and appropriate.

The error code is used throughout the codebase to throw errors with specific messages, ensuring clear and consistent error handling.

  • src/document/crdt/primitive.ts
  • src/document/crdt/tree.ts
  • src/document/crdt/counter.ts
  • src/api/converter.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new error code `Code.ErrUnimplemented`.

# Test: Search for the error code usage. Expect: Consistent handling of the new error code.
rg --type ts 'Code.ErrUnimplemented'

Length of output: 1072


Script:

#!/bin/bash
# Description: Verify how the error code `Code.ErrUnimplemented` is handled in the codebase.

# Test: Search for error handling logic related to `Code.ErrUnimplemented`.
rg --type ts 'Code.ErrUnimplemented' -A 5

Length of output: 3797


Line range hint 56-59:
LGTM! Verify the usage of the new error code.

The changes improve error specificity by using YorkieError with a specific error code Code.ErrUnimplemented.

However, ensure that the new error code Code.ErrUnimplemented is correctly used and handled throughout the codebase.

Verification successful

The usage of the new error code Code.ErrUnimplemented is appropriate and consistent throughout the codebase.

  • The error code is used with specific and clear messages in various files, which aids in debugging and maintaining the code.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new error code `Code.ErrUnimplemented`.

# Test: Search for the error code usage. Expect: Consistent handling of the new error code.
rg --type ts 'Code.ErrUnimplemented'

Length of output: 1072


Script:

#!/bin/bash
# Description: Gather context around the usage of `Code.ErrUnimplemented` in the identified files.

# Search for the usage of `Code.ErrUnimplemented` in `src/document/crdt/counter.ts` with context lines.
rg --type ts --context 5 'Code.ErrUnimplemented' src/document/crdt/counter.ts

# Search for the usage of `Code.ErrUnimplemented` in `src/document/crdt/primitive.ts` with context lines.
rg --type ts --context 5 'Code.ErrUnimplemented' src/document/crdt/primitive.ts

# Search for the usage of `Code.ErrUnimplemented` in `src/document/crdt/tree.ts` with context lines.
rg --type ts --context 5 'Code.ErrUnimplemented' src/document/crdt/tree.ts

# Search for the usage of `Code.ErrUnimplemented` in `src/api/converter.ts` with context lines.
rg --type ts --context 5 'Code.ErrUnimplemented' src/api/converter.ts

Length of output: 3914


868-868: LGTM! Verify the usage of the new error code.

The changes improve error specificity by using YorkieError with a specific error code Code.ErrInvalidType.

However, ensure that the new error code Code.ErrInvalidType is correctly used and handled throughout the codebase.

src/document/document.ts (5)

1773-1776: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to undo handle the new YorkieError appropriately.

Verification successful

LGTM! But verify the function usage in the codebase.

The code changes are approved.

All function calls to undo handle the new YorkieError appropriately, as confirmed by the extensive integration and unit tests.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `undo` handle the new `YorkieError` appropriately.

# Test: Search for the function usage. Expect: Appropriate handling of `YorkieError`.
rg --type ts -A 5 $'undo'

Length of output: 40919


856-859: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to subscribe handle the new YorkieError appropriately.


1873-1876: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to redo handle the new YorkieError appropriately.


1780-1783: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to undo handle the new YorkieError appropriately.


663-669: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to update handle the new YorkieError appropriately.

test/integration/primitive_test.ts Outdated Show resolved Hide resolved
src/document/crdt/element_rht.ts Outdated Show resolved Hide resolved
src/document/crdt/element_rht.ts Outdated Show resolved Hide resolved
src/document/operation/tree_edit_operation.ts Outdated Show resolved Hide resolved
src/document/crdt/rga_tree_split.ts Outdated Show resolved Hide resolved
src/document/crdt/rga_tree_list.ts Outdated Show resolved Hide resolved
src/document/crdt/rga_tree_list.ts Outdated Show resolved Hide resolved
src/document/crdt/rga_tree_list.ts Outdated Show resolved Hide resolved
src/document/crdt/rga_tree_list.ts Outdated Show resolved Hide resolved
src/util/splay_tree.ts Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 35be776 and 30330bb.

Files selected for processing (19)
  • src/document/crdt/element_rht.ts (3 hunks)
  • src/document/crdt/rga_tree_list.ts (4 hunks)
  • src/document/crdt/rga_tree_split.ts (4 hunks)
  • src/document/crdt/root.ts (2 hunks)
  • src/document/json/counter.ts (3 hunks)
  • src/document/json/text.ts (11 hunks)
  • src/document/json/tree.ts (24 hunks)
  • src/document/operation/add_operation.ts (2 hunks)
  • src/document/operation/edit_operation.ts (2 hunks)
  • src/document/operation/increase_operation.ts (2 hunks)
  • src/document/operation/move_operation.ts (2 hunks)
  • src/document/operation/remove_operation.ts (2 hunks)
  • src/document/operation/set_operation.ts (2 hunks)
  • src/document/operation/style_operation.ts (2 hunks)
  • src/document/operation/tree_edit_operation.ts (2 hunks)
  • src/document/operation/tree_style_operation.ts (2 hunks)
  • src/util/observable.ts (2 hunks)
  • src/util/splay_tree.ts (2 hunks)
  • test/integration/primitive_test.ts (2 hunks)
Files skipped from review as they are similar to previous changes (17)
  • src/document/crdt/element_rht.ts
  • src/document/crdt/rga_tree_list.ts
  • src/document/crdt/rga_tree_split.ts
  • src/document/crdt/root.ts
  • src/document/json/counter.ts
  • src/document/json/text.ts
  • src/document/json/tree.ts
  • src/document/operation/add_operation.ts
  • src/document/operation/edit_operation.ts
  • src/document/operation/increase_operation.ts
  • src/document/operation/move_operation.ts
  • src/document/operation/remove_operation.ts
  • src/document/operation/set_operation.ts
  • src/document/operation/style_operation.ts
  • src/document/operation/tree_edit_operation.ts
  • src/util/observable.ts
  • src/util/splay_tree.ts
Additional context used
Biome
test/integration/primitive_test.ts

[error] 41-41: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (3)
test/integration/primitive_test.ts (2)

6-6: New import statement is necessary.

The import of Code and YorkieError is required for the updated error handling approach.


39-43: Improved error handling with YorkieError.

The use of YorkieError with a specific error code and message enhances error handling.

Tools
Biome

[error] 41-41: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

src/document/operation/tree_style_operation.ts (1)

111-113: Improved error handling with YorkieError.

The use of YorkieError with a specific error code and message enhances error handling.

test/integration/primitive_test.ts Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 30330bb and b4c330d.

Files selected for processing (1)
  • test/helper/helper.ts (2 hunks)
Additional comments not posted (2)
test/helper/helper.ts (2)

39-39: Good practice: Importing YorkieError and Code.

The import of YorkieError and Code enhances error handling by providing structured error reporting.


70-75: Improvement: Enhanced error handling in waitAndVerifyNthEvent.

The use of YorkieError with Code.ErrInvalidArgument improves the specificity and clarity of the error message. The new format is more readable and provides better context.

@gwbaik9717
Copy link
Contributor Author

@chacha912

There are some points I'd like to share about the above PR:

  1. Unify naming convention for Error Code

I have revised the naming convention for error codes. Except for ok, I believe it is beneficial for all error code names to start with Err.

export enum Code {
  // Ok is returned when the operation completed successfully.
  Ok = 'ok',

  // ErrClientNotActivated is returned when the client is not active.
  ErrClientNotActivated = 'ErrClientNotActivated',

  // ErrDummy is returned when the error is intentional.
  ErrDummy = 'ErrDummy',
}
  1. Add New Error Codes ErrOperationNotReady and ErrOperationNotPermitted

I have added new Error codes ErrOperationNotReady, ErrOperationNotPermitted.

ErrOperationNotReady is an error when another event needs to be processed first. For example:

    fromIdx: number,
    toIdx: number,
    content: string,
    attributes?: A,
  ): [number, number] | undefined {
    if (!this.context || !this.text) {
      const ErrorMessage = 'Text is not initialized yet';
      logger.fatal(ErrorMessage);

      // initialization of `Text` must be preceded ahead of the operation
      throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage);
    }

ErrOperationNotPermitted is an error when operation is not permitted due to some reasons. For example:

  private undo(): void {
    if (this.isUpdating) {
      throw new YorkieError(
        Code.ErrOperationNotPermitted,
        'Undo is not allowed during an update',
      );
    }

However, I'm concerned that ErrOperationNotPermitted is too general and can be applied to any operations, potentially reducing its specificity. Therefore, I have made an effort to use it as sparingly as possible.

  private undo(): void {
    if (this.isUpdating) {
      throw new YorkieError(
        Code.ErrOperationNotPermitted,
        'Undo is not allowed during an update',
      );
    }
  1. Changes to YorkieError's toString method

I've modified the toString method of YorkieError to remove the YorkieError: prefix.

This change was made because when used with the logger, which already includes a YORKIE [Logger_Level]: prefix, it seemed redundant.

export class YorkieError extends Error {
  name = 'YorkieError';
  stack?: string;

  constructor(readonly code: Code, readonly message: string) {
    super(message);
    this.toString = (): string => `[code=${this.code}]: ${this.message}`;
  }
}
   if (err instanceof YorkieError) {
        logger.error(err.toString()); // Yorkie E: [code=ErrInvalidArgument]: message
      } else {
        logger.error(err);
      }
  1. Things to be done later...?

I haven't made changes in the devtools repository yet because it's a separate repository. I plan to address this in upcoming task (#648) related to monorepo improvements.

If you have any better suggestions, please let me know.

@hackerwins hackerwins requested a review from chacha912 July 30, 2024 09:25
Copy link
Contributor

@chacha912 chacha912 left a comment

Choose a reason for hiding this comment

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

Thank you for your work and detailed explanation.
I'm looking forward to the monorepo setup, which will allow us to work on the code simultaneously in devtools.🙌
I've left a few simple questions; please check the comments.

src/util/error.ts Outdated Show resolved Hide resolved
src/util/error.ts Outdated Show resolved Hide resolved
src/document/document.ts Outdated Show resolved Hide resolved
src/document/json/text.ts Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b4c330d and c694382.

Files selected for processing (9)
  • src/document/crdt/tree.ts (5 hunks)
  • src/document/document.ts (5 hunks)
  • src/document/json/counter.ts (3 hunks)
  • src/document/json/text.ts (11 hunks)
  • src/document/json/tree.ts (24 hunks)
  • src/document/operation/operation.ts (2 hunks)
  • src/util/error.ts (3 hunks)
  • src/util/index_tree.ts (22 hunks)
  • src/util/observable.ts (2 hunks)
Files skipped from review as they are similar to previous changes (8)
  • src/document/crdt/tree.ts
  • src/document/document.ts
  • src/document/json/text.ts
  • src/document/json/tree.ts
  • src/document/operation/operation.ts
  • src/util/error.ts
  • src/util/index_tree.ts
  • src/util/observable.ts
Additional comments not posted (2)
src/document/json/counter.ts (2)

82-84: LGTM! But verify the function usage in the codebase.

The code changes are approved. The error handling is now more structured and informative.

However, ensure that all function calls to increase handle the new YorkieError appropriately.


109-110: LGTM! But verify the function usage in the codebase.

The code changes are approved. The error handling is now more structured and informative.

However, ensure that all function calls to toJSForTest handle the new YorkieError appropriately.

@gwbaik9717 gwbaik9717 force-pushed the unify-error-throwing-methods branch from c694382 to ae7461b Compare August 3, 2024 04:48
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: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c694382 and ae7461b.

Files selected for processing (9)
  • src/document/crdt/tree.ts (5 hunks)
  • src/document/document.ts (5 hunks)
  • src/document/json/counter.ts (3 hunks)
  • src/document/json/text.ts (11 hunks)
  • src/document/json/tree.ts (24 hunks)
  • src/document/operation/operation.ts (2 hunks)
  • src/util/error.ts (3 hunks)
  • src/util/index_tree.ts (22 hunks)
  • src/util/observable.ts (2 hunks)
Files skipped from review as they are similar to previous changes (7)
  • src/document/crdt/tree.ts
  • src/document/document.ts
  • src/document/json/counter.ts
  • src/document/json/text.ts
  • src/document/operation/operation.ts
  • src/util/error.ts
  • src/util/observable.ts
Additional comments not posted (53)
src/document/json/tree.ts (28)

157-160: LGTM!

The function correctly throws a YorkieError with Code.ErrInvalidArgument for an empty text node value.


179-182: LGTM!

The function correctly throws a YorkieError with Code.ErrInvalidArgument for mixed node types.

Also applies to: 190-193


260-263: LGTM!

The getSize method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


274-277: LGTM!

The getNodeSize method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


288-291: LGTM!

The getIndexTree method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


302-305: LGTM!

The styleByPath method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


309-312: LGTM!

The styleByPath method correctly throws a YorkieError with Code.ErrInvalidArgument if the path is empty.


345-348: LGTM!

The style method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


352-355: LGTM!

The style method correctly throws a YorkieError with Code.ErrInvalidArgument if fromIdx is greater than toIdx.


394-397: LGTM!

The removeStyle method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


401-404: LGTM!

The removeStyle method correctly throws a YorkieError with Code.ErrInvalidArgument if fromIdx is greater than toIdx.


510-513: LGTM!

The editByPath method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


516-519: LGTM!

The editByPath method correctly throws a YorkieError with Code.ErrInvalidArgument if the path lengths are not equal.


522-525: LGTM!

The editByPath method correctly throws a YorkieError with Code.ErrInvalidArgument if the path is empty.


549-552: LGTM!

The editBulkByPath method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


555-558: LGTM!

The editBulkByPath method correctly throws a YorkieError with Code.ErrInvalidArgument if the path lengths are not equal.


561-564: LGTM!

The editBulkByPath method correctly throws a YorkieError with Code.ErrInvalidArgument if the path is empty.


583-586: LGTM!

The edit method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


589-592: LGTM!

The edit method correctly throws a YorkieError with Code.ErrInvalidArgument if fromIdx is greater than toIdx.


616-619: LGTM!

The editBulk method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


622-625: LGTM!

The editBulk method correctly throws a YorkieError with Code.ErrInvalidArgument if fromIdx is greater than toIdx.


639-642: LGTM!

The toXML method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


653-656: LGTM!

The toJSON method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


668-671: LGTM!

The toJSForTest method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


684-687: LGTM!

The toJSInfoForTest method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


698-701: LGTM!

The getRootTreeNode method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


712-715: LGTM!

The indexToPath method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.


726-729: LGTM!

The pathToIndex method correctly throws a YorkieError with Code.ErrNotInitialized if the tree is not initialized.

src/util/index_tree.ts (25)

133-133: LGTM!

The constructor correctly throws a YorkieError with Code.ErrRefused if a text node has children.


307-307: LGTM!

The append method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


323-323: LGTM!

The prepend method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


337-337: LGTM!

The insertBefore method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


342-342: LGTM!

The insertBefore method correctly throws a YorkieError with Code.ErrInvalidArgument if the reference child is not found.


354-354: LGTM!

The insertAfter method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


359-359: LGTM!

The insertAfter method correctly throws a YorkieError with Code.ErrInvalidArgument if the reference child is not found.


371-371: LGTM!

The insertAt method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


383-383: LGTM!

The removeChild method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


388-388: LGTM!

The removeChild method correctly throws a YorkieError with Code.ErrInvalidArgument if the child is not found.


439-439: LGTM!

The insertAfterInternal method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


444-444: LGTM!

The insertAfterInternal method correctly throws a YorkieError with Code.ErrInvalidArgument if the reference child is not found.


456-456: LGTM!

The insertAtInternal method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


469-469: LGTM!

The findOffset method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


493-493: LGTM!

The findBranchOffset method correctly throws a YorkieError with Code.ErrRefused if a text node has children.


581-584: LGTM!

The tokensBetween function correctly throws a YorkieError with Code.ErrInvalidArgument if from is greater than to.


588-591: LGTM!

The tokensBetween function correctly throws a YorkieError with Code.ErrInvalidArgument if from is out of range.


595-598: LGTM!

The tokensBetween function correctly throws a YorkieError with Code.ErrInvalidArgument if to is out of range.


676-679: LGTM!

The findTreePos function correctly throws a YorkieError with Code.ErrInvalidArgument if the index is out of range.


780-780: LGTM!

The findTextPos function correctly throws a YorkieError with Code.ErrInvalidArgument for an unacceptable path.


850-850: LGTM!

The treePosToPath method correctly throws a YorkieError with Code.ErrInvalidArgument for an invalid tree position.


874-874: LGTM!

The treePosToPath method correctly throws a YorkieError with Code.ErrInvalidArgument for an invalid tree position.


898-898: LGTM!

The pathToTreePos method correctly throws a YorkieError with Code.ErrInvalidArgument for an unacceptable path.


907-907: LGTM!

The pathToTreePos method correctly throws a YorkieError with Code.ErrInvalidArgument for an unacceptable path.


916-916: LGTM!

The pathToTreePos method correctly throws a YorkieError with Code.ErrInvalidArgument for an unacceptable path.

src/document/json/tree.ts Outdated Show resolved Hide resolved
src/document/json/tree.ts Outdated Show resolved Hide resolved
src/document/json/tree.ts Outdated Show resolved Hide resolved
src/document/json/tree.ts Outdated Show resolved Hide resolved
There is a problem of code being verbose with duuplicated logging and error throwing. Removing logger will temproarily resolve the issue.
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, codebase verification and nitpick comments (2)
src/document/crdt/element_rht.ts (2)

118-119: Fix variable naming convention.

The variable name ErrorMessage should follow camelCase convention.

- const ErrorMessage = `fail to find ${createdAt.toIDString()}`;
+ const errorMessage = `fail to find ${createdAt.toIDString()}`;

147-150: Fix variable naming convention.

The variable name ErrorMessage should follow camelCase convention.

- const ErrorMessage = `fail to find ${element.getCreatedAt().toIDString()}`;
+ const errorMessage = `fail to find ${element.getCreatedAt().toIDString()}`;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ae7461b and e6ae347.

Files selected for processing (20)
  • src/document/crdt/element_rht.ts (3 hunks)
  • src/document/crdt/rga_tree_list.ts (4 hunks)
  • src/document/crdt/rga_tree_split.ts (4 hunks)
  • src/document/crdt/root.ts (3 hunks)
  • src/document/document.ts (5 hunks)
  • src/document/json/counter.ts (3 hunks)
  • src/document/json/text.ts (11 hunks)
  • src/document/json/tree.ts (24 hunks)
  • src/document/operation/add_operation.ts (2 hunks)
  • src/document/operation/edit_operation.ts (2 hunks)
  • src/document/operation/increase_operation.ts (2 hunks)
  • src/document/operation/move_operation.ts (2 hunks)
  • src/document/operation/remove_operation.ts (2 hunks)
  • src/document/operation/set_operation.ts (2 hunks)
  • src/document/operation/style_operation.ts (2 hunks)
  • src/document/operation/tree_edit_operation.ts (2 hunks)
  • src/document/operation/tree_style_operation.ts (2 hunks)
  • src/util/logger.ts (1 hunks)
  • src/util/observable.ts (2 hunks)
  • src/util/splay_tree.ts (2 hunks)
Files skipped from review as they are similar to previous changes (15)
  • src/document/crdt/rga_tree_list.ts
  • src/document/crdt/rga_tree_split.ts
  • src/document/crdt/root.ts
  • src/document/json/counter.ts
  • src/document/json/text.ts
  • src/document/json/tree.ts
  • src/document/operation/add_operation.ts
  • src/document/operation/edit_operation.ts
  • src/document/operation/increase_operation.ts
  • src/document/operation/remove_operation.ts
  • src/document/operation/set_operation.ts
  • src/document/operation/tree_edit_operation.ts
  • src/document/operation/tree_style_operation.ts
  • src/util/observable.ts
  • src/util/splay_tree.ts
Additional comments not posted (11)
src/util/logger.ts (1)

94-98: LGTM! Verify the usage of the fatal method in the codebase.

The changes align with the PR objectives to separate logging and error throwing responsibilities.

However, ensure that all calls to the fatal method are reviewed to confirm they do not rely on the previous behavior of throwing an error.

Verification successful

Let's expand our search criteria to ensure we capture all potential usages of the fatal method.


To ensure thorough verification, let's search for the fatal method usage across all file types in the codebase.


The changes to the fatal method are verified.

The fatal method is not used elsewhere in the codebase, ensuring no dependencies rely on its previous behavior of throwing an error.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `fatal` method in the codebase.

# Test: Search for the method usage. Expect: Review all occurrences to ensure they do not rely on the previous behavior of throwing an error.
rg --type js -A 5 $'logger.fatal'

Length of output: 32


Script:

#!/bin/bash
# Description: Verify the usage of the `fatal` method in the codebase, including potential aliasing or different naming conventions.

# Test: Search for the method usage. Expect: Review all occurrences to ensure they do not rely on the previous behavior of throwing an error.
rg --type js -w 'fatal' -A 5

Length of output: 28


Script:

#!/bin/bash
# Description: Verify the usage of the `fatal` method in the codebase across all file types.

# Test: Search for the method usage. Expect: Review all occurrences to ensure they do not rely on the previous behavior of throwing an error.
rg -w 'fatal' -A 5

Length of output: 384

src/document/operation/move_operation.ts (2)

72-73: LGTM! Verify the error handling consistency.

The changes improve readability, maintainability, and consistency in error handling.

However, ensure that similar error handling patterns are consistently applied across the codebase.


68-69: LGTM! Verify the error handling consistency.

The changes improve readability, maintainability, and consistency in error handling.

However, ensure that similar error handling patterns are consistently applied across the codebase.

Verification successful

Error handling consistency verified.

The YorkieError is consistently used across the codebase for custom error handling, ensuring readability and maintainability.

  • Instances of YorkieError found in multiple files, including src/document, src/util, and src/client.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the consistency of error handling across the codebase.

# Test: Search for error handling patterns. Expect: Review similar patterns to ensure consistency.
rg --type js -A 5 $'throw new YorkieError'

Length of output: 43


Script:

#!/bin/bash
# Description: Verify the presence and consistency of error handling across the codebase.

# Search for the definition of YorkieError to understand its usage.
rg 'class YorkieError'

# Search for instances of YorkieError being thrown.
rg 'throw new YorkieError'

# Search for other error handling patterns to ensure consistency.
rg 'throw new'

Length of output: 26051

src/document/operation/style_operation.ts (2)

81-82: LGTM! Verify the error handling consistency.

The changes improve readability, maintainability, and consistency in error handling.

However, ensure that similar error handling patterns are consistently applied across the codebase.


85-86: LGTM! Verify the error handling consistency.

The changes improve readability, maintainability, and consistency in error handling.

However, ensure that similar error handling patterns are consistently applied across the codebase.

src/document/crdt/element_rht.ts (1)

20-20: LGTM! Import statement for YorkieError and Code.

The import statement is correct and necessary for the new error handling approach.

src/document/document.ts (5)

663-663: LGTM! Unified error throwing method.

The change to throw a YorkieError instead of a generic Error aligns with the objective of unifying error throwing methods.


850-853: LGTM! Improved error specificity.

The change to throw a YorkieError with a specific error code and message for invalid arguments improves the specificity and consistency of error handling.


1027-1027: LGTM! Improved error specificity.

The change to throw a YorkieError with a specific error code and message for invalid arguments improves the specificity and consistency of error handling.


1767-1777: LGTM! Improved error specificity.

The change to throw YorkieError instances with specific error codes and messages for different error conditions in the undo method improves the specificity and consistency of error handling.


1867-1878: LGTM! Improved error specificity.

The change to throw YorkieError instances with specific error codes and messages for different error conditions in the redo method improves the specificity and consistency of error handling.

@gwbaik9717
Copy link
Contributor Author

@chacha912

Thanks to your detailed explanation, I now have a better understanding of different error throwing cases. Based on the suggestions from above, I’ve made the following changes:

Firstly, I removed logger.fatal which was being used with throw new YorkieError for now due to its verbosity.

Secondly, I removed the logger.error part in the update method of Document in document.ts for the same reason: duplicated error logging.

// document.ts
try {
     // code skipped
} catch (err) {
      // removed logger.error
      throw err;
}

However, unlike logger.fatal, which was completely removed for now, I removed logger.error only in this specific instance. For example, the following code in client.ts uses logger.error and throws an error as well, but here, the logger provides some useful information that the default logger cannot.

// client.ts
.then((res) => {
  // code skipped
})
.catch((err) => {
  logger.error(`[DD] c:"${this.getKey()}" err:`, err);
  this.handleConnectError(err);
  throw err;
});

Lastly, I removed the unused message parameter from logger.fatal as @chacha912 suggested.

Please let me know, if there are any suggestions. :)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e6ae347 and d360393.

Files selected for processing (16)
  • src/document/crdt/element_rht.ts (3 hunks)
  • src/document/crdt/rga_tree_list.ts (4 hunks)
  • src/document/crdt/rga_tree_split.ts (5 hunks)
  • src/document/json/counter.ts (4 hunks)
  • src/document/json/tree.ts (25 hunks)
  • src/document/operation/add_operation.ts (3 hunks)
  • src/document/operation/edit_operation.ts (3 hunks)
  • src/document/operation/increase_operation.ts (2 hunks)
  • src/document/operation/move_operation.ts (2 hunks)
  • src/document/operation/remove_operation.ts (3 hunks)
  • src/document/operation/set_operation.ts (3 hunks)
  • src/document/operation/style_operation.ts (3 hunks)
  • src/document/operation/tree_edit_operation.ts (3 hunks)
  • src/document/operation/tree_style_operation.ts (3 hunks)
  • src/util/splay_tree.ts (2 hunks)
  • test/bench/tree.bench.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • test/bench/tree.bench.ts
Files skipped from review as they are similar to previous changes (14)
  • src/document/crdt/element_rht.ts
  • src/document/crdt/rga_tree_list.ts
  • src/document/crdt/rga_tree_split.ts
  • src/document/json/counter.ts
  • src/document/json/tree.ts
  • src/document/operation/add_operation.ts
  • src/document/operation/edit_operation.ts
  • src/document/operation/increase_operation.ts
  • src/document/operation/move_operation.ts
  • src/document/operation/set_operation.ts
  • src/document/operation/style_operation.ts
  • src/document/operation/tree_edit_operation.ts
  • src/document/operation/tree_style_operation.ts
  • src/util/splay_tree.ts
Additional comments not posted (4)
src/document/operation/remove_operation.ts (4)

32-32: Imports look good.

The new imports Code and YorkieError are necessary for the improved error handling.


71-72: Improved error handling.

Throwing YorkieError with a specific error code and message improves clarity and consistency.


75-76: Improved error handling.

Throwing YorkieError with a specific error code and message improves clarity and consistency.


71-76: Verify consistent error handling across the codebase.

Ensure that the new error handling approach is consistently applied across the codebase.

Verification successful

Verification successful: The error handling approach is consistent across the codebase.

The instances of throw new YorkieError follow a consistent pattern of using specific error codes and messages, ensuring uniform error handling.

  • src/util/splay_tree.ts
  • src/util/observable.ts
  • src/util/index_tree.ts
  • src/document/document.ts
  • src/document/operation/tree_style_operation.ts
  • src/document/operation/tree_edit_operation.ts
  • src/document/operation/style_operation.ts
  • src/document/json/text.ts
  • src/document/json/tree.ts
  • src/document/json/object.ts
  • src/document/operation/set_operation.ts
  • src/document/operation/operation.ts
  • src/document/operation/increase_operation.ts
  • src/document/operation/edit_operation.ts
  • src/document/json/counter.ts
  • src/document/operation/move_operation.ts
  • src/document/crdt/primitive.ts
  • src/document/crdt/tree.ts
  • src/document/operation/remove_operation.ts
  • src/document/crdt/rga_tree_split.ts
  • src/document/crdt/element_rht.ts
  • src/document/crdt/rga_tree_list.ts
  • src/document/operation/add_operation.ts
  • src/api/converter.ts
  • src/client/client.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent error handling across the codebase.

# Test: Search for instances of YorkieError usage. Expect: Consistent usage of YorkieError with specific error codes and messages.
rg --type-add 'ts' --type 'ts' 'throw new YorkieError'

Length of output: 112


Script:

#!/bin/bash
# Description: Verify consistent error handling across the codebase.

# Test: Search for instances of YorkieError usage. Expect: Consistent usage of YorkieError with specific error codes and messages.
rg --glob '*.ts' 'throw new YorkieError'

Length of output: 12373

Copy link
Contributor

@chacha912 chacha912 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!
This looks good to me :)

@hackerwins hackerwins merged commit 39d7f62 into yorkie-team:main Aug 7, 2024
2 checks passed
hackerwins pushed a commit that referenced this pull request Aug 7, 2024
This commit unifies error throwing methods and removes throw error in
the logger.

Previously, implementation of the logger includes both logging and error
throwing, which can lead to confusion and type problems. By separating
these concerns, we can ensure that the logger is only responsible for
logging messages, while errors are thrown explicitly using `YorkieError`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Unify Error Throwing Methods
3 participants