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

grpc error management like kratos error generator #4076

Open
Danny5487401 opened this issue Apr 16, 2024 · 3 comments
Open

grpc error management like kratos error generator #4076

Danny5487401 opened this issue Apr 16, 2024 · 3 comments

Comments

@Danny5487401
Copy link

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"
    }
}
@Danny5487401 Danny5487401 changed the title grpc error support grpc error management like kratos error generator Apr 16, 2024
@harshithsaiv
Copy link

harshithsaiv commented Apr 16, 2024

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",
    }
}

@Danny5487401
Copy link
Author

Danny5487401 commented Apr 17, 2024

@harshithsaiv errorx.BusinessError struct dones not exist in module(github.com/zeromicro/go-zero/core/errorx ).

@Issues-translate-bot
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants