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

🐛 returns rejected Promise instead throws Error #19

Merged
merged 1 commit into from
May 2, 2024
Merged

🐛 returns rejected Promise instead throws Error #19

merged 1 commit into from
May 2, 2024

Conversation

Milly
Copy link
Contributor

@Milly Milly commented May 2, 2024

It is a bad pattern to throw an error in a function whose return type is Promise.
When to handle multiple promises such as Promise.all([...]), if there is a function that throws an error, unhandledrejection will occur due to the rejection of other Promises.

In Client.prototype.call(), when session is closed, this.#recv(...) returns a rejected Promise and this.#session.send(...) throws an Error, then the rejected Promise is not handled anyone and unhundledrejection is occured.

const [ret, _] = await Promise.all([
this.#recv(msgid),
this.#session.send(command),
]);

Unhandled rejection: Error: Session is not running
    at Session.recv (file:///work/vim/deno-vim-channel-command/session.ts:93:13)
    at Client.#recv (file:///work/vim/deno-vim-channel-command/client.ts:67:47)
    at Client.call (file:///work/vim/deno-vim-channel-command/client.ts:149:17)
    at Vim.call (file:///work/vim/denops.vim/denops/@denops-private/host/vim.ts:51:39)
    ...

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling in session management functions to ensure users receive clear feedback when attempting operations on non-running sessions.

It is a bad pattern to throw an error in a function whose return type is
Promise. When to handle multiple promises such as `Promise.all([...])`,
if there is a function that throws an error, unhandledrejection will
occur due to the rejection of other Promises.

In `Client.prototype.call()`, when session is closed, `this.#recv(...)`
returns a rejected Promise and `this.#session.send(...)` throws an Error,
then the rejected Promise is not handled anyone and unhundledrejection
is occured.

```
Unhandled rejection: Error: Session is not running
    at Session.recv (file:///work/vim/deno-vim-channel-command/session.ts:93:13)
    at Client.#recv (file:///work/vim/deno-vim-channel-command/client.ts:67:47)
    at Client.call (file:///work/vim/deno-vim-channel-command/client.ts:149:17)
    at Vim.call (file:///work/vim/denops.vim/denops/@denops-private/host/vim.ts:51:39)
    ...
```
@Milly Milly added the bug Something isn't working label May 2, 2024
@Milly Milly requested a review from lambdalisue May 2, 2024 12:09
Copy link

coderabbitai bot commented May 2, 2024

Walkthrough

The recent updates to the Session class in the TypeScript codebase primarily involve added error handling. Now, promises will be rejected with an error if operations are attempted on a non-running session. Corresponding changes in test files replace assertThrows with assertRejects to accommodate these new asynchronous error handling mechanisms.

Changes

File(s) Change Summary
session.ts Enhanced error handling: Promises reject with an error if operations (send, recv, wait, shutdown, forceShutdown) are invoked on a non-running session.
session_test.ts Updated tests to use assertRejects for handling asynchronous errors in response to the updated error handling in session.ts.

🐰✨
A hop, a skip, a code deploy,
In the land of bugs, there's newfound joy.
When sessions pause, errors we employ,
A safer net, for every girl and boy.
CodeRabbit bounds, with every fix deployed! 🚀🥕


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 6729a74 and abe5762.
Files selected for processing (2)
  • session.ts (3 hunks)
  • session_test.ts (5 hunks)
Additional comments not posted (10)
session.ts (5)

73-78: Changes to the send method correctly implement the desired behavior of returning a rejected Promise when the session is not running. This aligns with the PR objectives to handle errors more consistently in asynchronous operations.


86-92: Changes to the recv method correctly implement the desired behavior of returning a rejected Promise under error conditions. This aligns with the PR objectives to handle errors more consistently in asynchronous operations.


159-164: Changes to the wait method correctly implement the desired behavior of returning a rejected Promise when the session is not running. This aligns with the PR objectives to handle errors more consistently in asynchronous operations.


172-177: Changes to the shutdown method correctly implement the desired behavior of returning a rejected Promise when the session is not running. This aligns with the PR objectives to handle errors more consistently in asynchronous operations.


187-192: Changes to the forceShutdown method correctly implement the desired behavior of returning a rejected Promise when the session is not running. This aligns with the PR objectives to handle errors more consistently in asynchronous operations.

session_test.ts (5)

51-51: The updated test case for Session.send correctly uses assertRejects to check for the rejected Promise when the session is not started. This aligns with the changes made in the Session class.


98-98: The updated test case for Session.recv correctly uses assertRejects to check for the rejected Promise when the session is not started. This aligns with the changes made in the Session class.


164-164: The updated test case for Session.wait correctly uses assertRejects to check for the rejected Promise when the session is not started. This aligns with the changes made in the Session class.


203-203: The updated test case for Session.shutdown correctly uses assertRejects to check for the rejected Promise when the session is not started. This aligns with the changes made in the Session class.


254-254: The updated test case for Session.forceShutdown correctly uses assertRejects to check for the rejected Promise when the session is not started. This aligns with the changes made in the Session class.


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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration 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
Member

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

Thanks!

@lambdalisue lambdalisue merged commit f7ef870 into vim-denops:main May 2, 2024
@Milly Milly deleted the rejects-instead-throws branch May 3, 2024 05:28
@Milly
Copy link
Contributor Author

Milly commented May 3, 2024

Note that https://github.com/lambdalisue/deno-messagepack-rpc has same issue.

@Milly Milly mentioned this pull request May 3, 2024
lambdalisue added a commit to vim-denops/denops.vim that referenced this pull request May 9, 2024
lambdalisue added a commit to vim-denops/denops.vim that referenced this pull request May 9, 2024
lambdalisue added a commit to vim-denops/denops.vim that referenced this pull request May 9, 2024
lambdalisue added a commit to vim-denops/denops.vim that referenced this pull request May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants