Update error response status code in EnsureMemberClusterMiddleware#439
Update error response status code in EnsureMemberClusterMiddleware#439RafsanNeloy wants to merge 1 commit into
Conversation
Signed-off-by: rafsanneloy <rafsanneloy@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello @RafsanNeloy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves an issue in the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request correctly updates the error response in EnsureMemberClusterMiddleware to use http.StatusInternalServerError instead of http.StatusOK. This is a good step towards proper HTTP status code usage. However, there are opportunities to further improve error specificity (e.g., handling 404 Not Found) and maintain consistency with other middlewares in the same file. Note that there is a typo in the PR title ('pdate').
| c.AbortWithStatusJSON(http.StatusInternalServerError, common.BaseResponse{ | ||
| Code: http.StatusInternalServerError, |
There was a problem hiding this comment.
Returning http.StatusInternalServerError (500) is an improvement over http.StatusOK (200) for errors. However, since this middleware checks for the existence of a cluster, a 'Not Found' error from the Kubernetes client should ideally result in an http.StatusNotFound (404) response rather than a 500.
Furthermore, this change makes this middleware inconsistent with ClientMiddleware (lines 76 and 84), which uses common.Fail. common.Fail currently returns http.StatusOK. It would be better to unify the error handling strategy across the project, perhaps by updating common.Fail to accept an HTTP status code.
There was a problem hiding this comment.
can we write like
k8serrors "k8s.io/apimachinery/pkg/api/errors"
func Response(c *gin.Context, err error, data interface{}) {
status := http.StatusOK
code := http.StatusOK // biz status code
message := "success" // biz status message
if err != nil {
message = err.Error()
status = http.StatusInternalServerError
code = http.StatusInternalServerError
var apiErr k8serrors.APIStatus
if errors.As(err, &apiErr) {
status = int(apiErr.Status().Code)
if status == 0 {
status = http.StatusInternalServerError
}
code = status
}
}
c.JSON(status, BaseResponse{
Code: code,
Msg: message,
Data: data,
})
}
so that we can call in EnsureMemberClusterMiddleware, middleware.go -
func EnsureMemberClusterMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
karmadaClient := client.InClusterKarmadaClient()
_, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), c.Param("clustername"), metav1.GetOptions{})
if err != nil {
common.Fail(c, err)
c.Abort()
return
}
c.Next()
}
}
There was a problem hiding this comment.
@RafsanNeloy thanks for your contribution 👍 . And your suggestion is very good, i think here we can simplify it
common.Fail(c, err)
c.Abort()
returnanother suggestion, I think we can provide more helper method like FaileWithNotAuthorized、FailWithNotFound on top of common.Fail, provide differen kinds of http response with different http code
There was a problem hiding this comment.
another different opinion, I think biz code should not reuse http code, they have different meaning ~
|
/cc @warjiang |
|
/assign |
What type of PR is this?
/kind bug
What this PR does / why we need it:
Which issue(s) this PR fixes: Altered
AbortWithStatusJSONtonow correctly usehttp.StatusInternalServerErroras both the HTTP status code and the internal code for the payload, ensuring accurate HTTP-layer error propagationFixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: