-
Notifications
You must be signed in to change notification settings - Fork 66
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
Conversation
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. WalkthroughThe recent changes in the Changes
Poem
TipsChat with CodeRabbit Bot (
|
There was a problem hiding this 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
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 typeRequestInterceptorFunc
is added to theClient
struct. This field is used in thePost
method to conditionally execute a custom function if it's set.
|
||
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) | ||
} |
There was a problem hiding this comment.
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.
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) | |
} |
clientv2/client.go
Outdated
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) |
There was a problem hiding this comment.
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.
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) |
clientv2/client.go
Outdated
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) |
There was a problem hiding this comment.
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.
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) |
clientv2/client.go
Outdated
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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:
} | |
} | |
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 | |
} | |
} |
There was a problem hiding this 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
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 theClient
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 useCustomDo
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 usingerrors.As
. This is a good practice as it allows for type assertion of errors.
clientv2/client.go
Outdated
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) |
There was a problem hiding this comment.
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.
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) |
clientv2/client.go
Outdated
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) |
There was a problem hiding this comment.
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.
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) |
Summary by CodeRabbit