grpc error management like kratos error generator
is there any tools like https://github.com/go-kratos/kratos/tree/main/cmd/protoc-gen-go-errors that kratos does?
{
// The error code is consistent with HTTP status and can be converted into grpc status in grpc.
"code": 500,
// The error reason is defined as the business judgment error code.
"reason": "USER_NOT_FOUND",
// Error information is user-readable information and can be used as user prompt content.
"message": "invalid argument error",
// Error meta information, add additional extensible information for the error.
"metadata": {
"foo": "bar"
}
}
I think in go-zero we do not have a direct equivalent to Kratos' as a built in tool. Go-zero has its own mechanism and tools for managing errors through its 'errorx' package.
The following code sums up the error handling :
package errors
import (
"net/http"
"github.com/zeromicro/go-zero/core/errorx"
)
var ErrUserNotFound = errorx.BusinessError{
Code: http.StatusNotFound,
Message: "The specified user was not found.",
Reason: "USER_NOT_FOUND",
Metadata: map[string]string{
"foo": "bar",
},
}
func HandleErrors(err error) (int, interface{}) {
if bizErr, ok := err.(errorx.BusinessError); ok {
return bizErr.Code, bizErr
}
return http.StatusInternalServerError, errorx.BusinessError{
Code: http.StatusInternalServerError,
Message: err.Error(),
Reason: "INTERNAL_ERROR",
}
}
@harshithsaiv errorx.BusinessError struct dones not exist in module(github.com/zeromicro/go-zero/core/errorx ).
Bot detected the issue body's language is not English, translate it automatically. π―ππ»π§βπ€βπ§π«π§πΏβπ€βπ§π»π©πΎβπ€βπ¨πΏπ¬πΏ
@harshithsaiv error.Business Error struct does not exist in github.com/zeromicro/go-zero/core/errorx module