Skip to content

Update error response status code in EnsureMemberClusterMiddleware#439

Open
RafsanNeloy wants to merge 1 commit into
karmada-io:mainfrom
RafsanNeloy:middleware
Open

Update error response status code in EnsureMemberClusterMiddleware#439
RafsanNeloy wants to merge 1 commit into
karmada-io:mainfrom
RafsanNeloy:middleware

Conversation

@RafsanNeloy
Copy link
Copy Markdown
Contributor

@RafsanNeloy RafsanNeloy commented Feb 22, 2026

What type of PR is this?
/kind bug

What this PR does / why we need it:

Which issue(s) this PR fixes: Altered AbortWithStatusJSON tonow correctly use http.StatusInternalServerError as both the HTTP status code and the internal code for the payload, ensuring accurate HTTP-layer error propagation
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Signed-off-by: rafsanneloy <rafsanneloy@gmail.com>
@karmada-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign samzong for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @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 EnsureMemberClusterMiddleware where an incorrect HTTP status code was being returned for internal server errors. The modification ensures that API clients receive an accurate 500 Internal Server Error response when a cluster cannot be retrieved, which enhances API reliability and client-side error handling.

Highlights

  • Error Handling: Corrected the HTTP status code returned by EnsureMemberClusterMiddleware from 200 OK to 500 Internal Server Error when a cluster lookup fails, ensuring proper error communication to clients.
Changelog
  • cmd/api/app/router/middleware.go
    • Updated the HTTP status code and the response body's error code to 500 Internal Server Error for failed cluster lookups.
Activity
  • No specific activity has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@karmada-bot karmada-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Feb 22, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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').

Comment on lines +44 to +45
c.AbortWithStatusJSON(http.StatusInternalServerError, common.BaseResponse{
Code: http.StatusInternalServerError,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@warjiang
In response.go

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()
	}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@RafsanNeloy thanks for your contribution 👍 . And your suggestion is very good, i think here we can simplify it

common.Fail(c, err)
c.Abort()
return

another suggestion, I think we can provide more helper method like FaileWithNotAuthorizedFailWithNotFound on top of common.Fail, provide differen kinds of http response with different http code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

another different opinion, I think biz code should not reuse http code, they have different meaning ~

@RafsanNeloy RafsanNeloy changed the title pdate error response status code in EnsureMemberClusterMiddleware Update error response status code in EnsureMemberClusterMiddleware Feb 22, 2026
@RafsanNeloy
Copy link
Copy Markdown
Contributor Author

/cc @warjiang

@karmada-bot karmada-bot requested a review from warjiang February 22, 2026 13:49
@warjiang
Copy link
Copy Markdown
Contributor

warjiang commented Mar 2, 2026

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants