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

Add try catch to connect() #2674

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
Open

Conversation

ygpark80
Copy link

@ygpark80 ygpark80 commented Apr 24, 2024

Information

Type Breaking change
Fix No

As I mentioned in this comment, an error can arise during the connect() function and render the component useless. We need to add a try-catch block. In my case, I was getting an ETIMEDOUT error.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling during the connection process in the database management system.

Copy link

coderabbitai bot commented Apr 24, 2024

Walkthrough

The recent update introduces a safeguard in the connection setup process within the ORM package. A try-catch block has been added around the connection initiation step to effectively handle any errors that might occur, enhancing the robustness and reliability of the database interactions.

Changes

File Path Change Summary
packages/orm/ioredis/src/utils/registerConnectionProvider.ts Added try-catch block around await connection.connect() for error handling.

🐰✨
In the realm of code where the data hops,
A tiny change in the script now stops,
Errors that lurk in the shadowy night,
With a try and a catch, we set it right!
Hop, hop, hurray to safer connections tonight! 🌟
🐰✨


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between fce493f and bb2fe9a.
Files selected for processing (1)
  • packages/orm/ioredis/src/utils/registerConnectionProvider.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/orm/ioredis/src/utils/registerConnectionProvider.ts

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

@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: 1

@Romakita
Copy link
Collaborator

@ygpark80 thanks forvthe PR. Just rework your commit message please (follow the angular commit convention pls ;))
See you

@Romakita
Copy link
Collaborator

@@ -69,7 +69,9 @@ export function registerConnectionProvider({provide, name = "default"}: CreateCo
} as RedisOptions);
}

await connection.connect();
try {
Copy link
Collaborator

Choose a reason for hiding this comment

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

But adding this try catch will hide the connection error. I’m not favorable to do add that without loing the error.
Also I prefer to catch the error only if an option is enable. By default, the error must be thrown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion:

        const error = await catchAsyncError(() => connection.connect());

        if (error && !connectionFailSilently) {
          throw error
        }
export interface BaseIORedisConfiguration {
  name?: TokenProvider;
  cache?: boolean;
  connectionFailSilently?: boolean; // add this option
}
  • add documentation on ioredis.md page to describe this new option (and your usecase can help to understand why this option exists)
  • add unit test to cover this new usecase :)

Copy link
Author

Choose a reason for hiding this comment

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

I'm not a big fan of creating lots of options since ioredis already has tons. lol. If this is used in a serverless environment, not all resources may be available on startup. However, registerConnectionProvider uses lazyConnect: true and tries to connect to Redis during component initialization. But if it fails, the whole server won't start up, which I think is a bigger problem than losing the error.

I think there is another option: do not use lazyConnect: true and leave ioredis as is. ioredis will use the auto-reconnect feature if an incident like mine happens. You don't have to wait until the connection is ready because ioredis already handles (queues) commands internally.

I think Ts.ED should not let people have too many options, as developers should just go ahead and write logic. Adding an option, in my humble opinion, is unnecessary and also confusing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I understand your point, but lazyConnect is here to solve another issue, and it's normal to stop the bootstrap server when the redis connection isn't available, because this problem will cause other issuer later on the runtime.

We can use the lazyConnect option to address your issue and keep the existing behavior as is it.

        const {name, cache, lazyConnect = true, ...redisOptions} = item;
//...
          connection = new Redis.Cluster(nodes, {
            ...clusterOptions,
            lazyConnect // replace all lazyConnect in the provider
          });
//...
       if (lazyConnect) {
         await connection.connect();
      }

I think this is the better option for you and me :D

See you

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. I'll do some work sometime this week.

Copy link
Collaborator

Choose a reason for hiding this comment

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

let me know when it's ok for you ;)

@@ -69,7 +69,9 @@ export function registerConnectionProvider({provide, name = "default"}: CreateCo
} as RedisOptions);
}

await connection.connect();
try {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion:

        const error = await catchAsyncError(() => connection.connect());

        if (error && !connectionFailSilently) {
          throw error
        }
export interface BaseIORedisConfiguration {
  name?: TokenProvider;
  cache?: boolean;
  connectionFailSilently?: boolean; // add this option
}
  • add documentation on ioredis.md page to describe this new option (and your usecase can help to understand why this option exists)
  • add unit test to cover this new usecase :)

@ygpark80
Copy link
Author

@ygpark80 thanks forvthe PR. Just rework your commit message please (follow the angular commit convention pls ;)) See you

Sorry about that. it has been updated.

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

Successfully merging this pull request may close these issues.

None yet

2 participants