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

The HTTP status is 101 instead of 200 When my errors.code is set to 101 #3553

Closed
byteweap opened this issue Feb 21, 2025 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@byteweap
Copy link

byteweap commented Feb 21, 2025

What happened:

The HTTP status is 101 instead of 200 When my errors.code is set to 101 and trigger this error, example:
enum Error {
option (errors.default_code) = 500;
INVALID_REQUEST_PARAM = 0 [(errors.code) = 101];
}

What you expected to happen:

The HTTP status is 200 and response data is
'{
"code": 101,
"reason": "INVALID_REQUEST_PARAM",
"message": "",
"metadata": {}
}'

How to reproduce it (as minimally and precisely as possible):

Set your errors.code is 101 in error.proto and trigger it

Environment:

  • Kratos version (use kratos -v): v2.8.3
  • Go version (use go version): go1.23.4
  • OS (e.g: cat /etc/os-release): Ubuntu 24.04.1 LTS
  • protoc-gen-go version: v1.36.5
  • protoc version: v3.21.12
@byteweap byteweap added the bug Something isn't working label Feb 21, 2025
Copy link

dosubot bot commented Feb 21, 2025

Hey there, @byteweap! I'm here to help you with any bugs, questions, or contributions. Let's work together to solve the issue you're experiencing.

In Kratos, when you set a custom error code in Protobuf, the framework maps these error codes to HTTP status codes. It seems that in your case, the error code 101 is being directly mapped to the HTTP status code 101, which is not the behavior you expect.

To achieve the desired behavior where the HTTP status is 200 while still returning your custom error code and message, you may need to customize the error handling logic. Kratos provides a DefaultErrorEncoder function that you can override to customize how errors are encoded into HTTP responses. By implementing your own error encoder, you can control the HTTP status code independently of the error code.

Here's a basic example of how you might implement a custom error encoder:

func CustomErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
    se := errors.FromError(err)
    // Set the HTTP status code to 200
    w.WriteHeader(http.StatusOK)
    // Encode the error response
    json.NewEncoder(w).Encode(map[string]interface{}{
        "code":     se.Code,
        "reason":   se.Reason,
        "message":  se.Message,
        "metadata": se.Metadata,
    })
}

You would then configure your HTTP server to use this custom error encoder. This way, you can ensure that the HTTP status code is always 200, regardless of the custom error code set in your Protobuf definition.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@byteweap byteweap reopened this Feb 21, 2025
@byteweap
Copy link
Author

Please delete this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant