go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

grpc error management like kratos error generator

Open Danny5487401 opened this issue 1 year ago β€’ 3 comments

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 avatar Apr 16 '24 03:04 Danny5487401

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 avatar Apr 16 '24 18:04 harshithsaiv

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

Danny5487401 avatar Apr 17 '24 02:04 Danny5487401

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

Issues-translate-bot avatar Apr 17 '24 02:04 Issues-translate-bot