You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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:
funcCustomErrorEncoder(w http.ResponseWriter, r*http.Request, errerror) {
se:=errors.FromError(err)
// Set the HTTP status code to 200w.WriteHeader(http.StatusOK)
// Encode the error responsejson.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.
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 -v
): v2.8.3go version
): go1.23.4cat /etc/os-release
): Ubuntu 24.04.1 LTSThe text was updated successfully, but these errors were encountered: