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 customDo function #192

Merged
merged 3 commits into from
Nov 9, 2023
Merged

add customDo function #192

merged 3 commits into from
Nov 9, 2023

Conversation

Yamashou
Copy link
Owner

@Yamashou Yamashou commented Nov 9, 2023

Summary by CodeRabbit

  • New Features
    • Introduced a customizable client request handling.
  • Bug Fixes
    • Enhanced error handling in various methods for improved stability and debugging.
  • Refactor
    • Updated several methods for better error management and response handling.

Copy link

coderabbitai bot commented Nov 9, 2023

Warning

Rate Limit Exceeded

@Yamashou has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 7 minutes and 6 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per repository.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 2fa0ff4 and 22c1064.

Walkthrough

The recent changes in the clientv2/client.go file have significantly improved error handling and added a new feature. The Client struct now includes a CustomDo field, and the Post method has been updated to use this field conditionally. Error handling has been enhanced in the prepareMultipartFormBody, do, and parseResponse methods.

Changes

File Change Summary
clientv2/client.go Added errors and log package imports. Added CustomDo field to Client struct. Modified Post, prepareMultipartFormBody, do, and parseResponse methods for improved error handling.

Poem

🍂 As the leaves fall, code changes call, 🍁

In the client.go, we've had a ball. 🎉

With CustomDo added, and errors well padded, 🐇

Our code's robustness has been upgraded. 💪

On this day in history, the Berlin Wall fell, 🏰

Just like the bugs, we bid farewell. 🐞

So here's to the coders, in all nations, 🌍

Celebrating our inclusive creations. 🎊


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0117508 and 7fbb2ea.
Files selected for processing (1)
  • clientv2/client.go (6 hunks)
Additional comments: 2
clientv2/client.go (2)
  • 8-8: The errors package is imported to facilitate error handling. Ensure that it is used correctly throughout the code.

  • 55-60: A new field CustomDo of type RequestInterceptorFunc is added to the Client struct. This field is used in the Post method to conditionally execute a custom function if it's set.

Comment on lines 204 to 213

f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)

// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}

return f(ctx, req, gqlInfo, respData, c.do)
}
Copy link

Choose a reason for hiding this comment

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

The Post method is updated to conditionally use CustomDo if it's set. This provides flexibility to the client to use a custom function for handling requests.

+  // if custom do is set, use it instead of the default one
+  if c.CustomDo != nil {
+    return f(ctx, req, gqlInfo, respData, c.CustomDo)
+  }
+  return f(ctx, req, gqlInfo, respData, c.do)

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)
// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}
return f(ctx, req, gqlInfo, respData, c.do)
}
f := ChainInterceptor(append([]RequestInterceptor{c.RequestInterceptor}, interceptors...)...)
// if custom do is set, use it instead of the default one
if c.CustomDo != nil {
return f(ctx, req, gqlInfo, respData, c.CustomDo)
}
return f(ctx, req, gqlInfo, respData, c.do)
}

Comment on lines 268 to 275
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
}
}(writer)
Copy link

Choose a reason for hiding this comment

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

The prepareMultipartFormBody function now handles the error from writer.Close(). However, the error is not being used or returned. It's recommended to handle the error properly.

-  if err != nil {
-  }
+  if err != nil {
+    // handle or return the error
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
}
}(writer)
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
// handle or return the error
}
}(writer)

Comment on lines 314 to 321
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
Copy link

Choose a reason for hiding this comment

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

The do method has been updated to handle errors from Body.Close(). However, the error is not being used or returned. It's recommended to handle the error properly.

-  if err != nil {
-  }
+  if err != nil {
+    // handle or return the error
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
// handle or return the error
}
}(resp.Body)

Comment on lines 345 to 357
}
}

fmt.Println("body", string(body))

// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
if gqlErr, ok := err.(*GqlErrorList); ok {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
} else if !isKOCode { // if is KO code there is already the http error, this error should not be returned
return err
}
}

Copy link

Choose a reason for hiding this comment

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

The parseResponse method in the Client struct has been modified to handle errors using errors.As. This is a good practice as it allows for more specific error handling.

+  var gqlErr *GqlErrorList
+  if errors.As(err, &gqlErr) {
+    errResponse.GqlErrors = &gqlErr.Errors
+  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Your code snippet already contains the lines from the diff snippet. Therefore, no changes are needed. Here is your original code:

Suggested change
}
}
fmt.Println("body", string(body))
// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
if gqlErr, ok := err.(*GqlErrorList); ok {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
} else if !isKOCode { // if is KO code there is already the http error, this error should not be returned
return err
}
}
}
}
fmt.Println("body", string(body))
// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := c.unmarshal(body, result); err != nil {
var gqlErr *GqlErrorList
if errors.As(err, &gqlErr) {
errResponse.GqlErrors = &gqlErr.Errors
}
}

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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7fbb2ea and 2fa0ff4.
Files selected for processing (1)
  • clientv2/client.go (6 hunks)
Additional comments: 4
clientv2/client.go (4)
  • 8-11: The addition of "errors" and "log" packages is a good practice for error handling and logging.

  • 56-61: The addition of the CustomDo field to the Client struct allows for custom request handling. Ensure that this field is properly initialized and used throughout the codebase.

  • 205-214: The Post method is updated to conditionally use CustomDo if it's set. This provides flexibility to the client to use a custom function for handling requests.

  • 350-358: The parseResponse method has been updated to handle errors using errors.As. This is a good practice as it allows for type assertion of errors.

Comment on lines 269 to 277
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
log.Fatal(err)
}
}(writer)
Copy link

Choose a reason for hiding this comment

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

The error from writer.Close() is now being handled. However, using log.Fatal(err) will cause the program to terminate, which might not be the desired behavior. Consider returning the error instead.

- log.Fatal(err)
+ return err

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer writer.Close()
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
log.Fatal(err)
}
}(writer)
buffer *bytes.Buffer, formFields []FormField, files []MultipartFilesGroup,
) (string, error) {
writer := multipart.NewWriter(buffer)
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
return err
}
}(writer)

Comment on lines 316 to 324
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)
Copy link

Choose a reason for hiding this comment

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

The error from Body.Close() is now being handled. However, using log.Fatal(err) will cause the program to terminate, which might not be the desired behavior. Consider returning the error instead.

- log.Fatal(err)
+ return err

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer func(Body io.ReadCloser) error {
err := Body.Close()
if err != nil {
return err
}
return nil
}(resp.Body)

@Yamashou Yamashou merged commit c8ef9d8 into master Nov 9, 2023
1 check failed
@Yamashou Yamashou deleted the add-custom-do branch November 9, 2023 03:41
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.

1 participant