Go-micro: Unable to handle error

Created on 31 Jan 2019  ·  3Comments  ·  Source: asim/go-micro

Hello.
Im trying to use “google.golang.org/grpc/status” with go-micro, but facing problems.
Check snippet below.

// Server 
import "google.golang.org/grpc/codes"
func (srv *userRPCHandler) CreateUser(ctx context.Context, in *userPb.CreateUserRequest, out *userPb.CreateUserResponse) error {
 ...
 return status.Error(codes.NotFound, "id was not found")
 ...
}

// Client
import  "google.golang.org/grpc/status"
usr, err := h.client.GetUserByID(c, &userP.GetUserByIDRequest{UserID: userID})
if err != nil {
    if st, ok := status.FromError(err); ok {
    fmt.Print(st.Code())
    } else {
    fmt.Printf("not able to parse error returned %v", err)
    }
}

status.FormError(err) never able to parse err as status.statusError. Always returns error Unknown
As i understand go-micro might not parse some metadata from response or i am doing something wrong?
If its really micro's handling way, is there was a case why it was done?

Most helpful comment

@P1sar I am facing the same problem when trying to integrate with go-micro. The client-side error handling is totally messed up. Did you solve this problem? Thanks in advance!

All 3 comments

Go Micro does not support grpc by default. If you are using grpc plugins please file the issue under go-plugins or look at https://github.com/micro/go-plugins/tree/master/server/grpc to understand how it handles the error.

@P1sar I am facing the same problem when trying to integrate with go-micro. The client-side error handling is totally messed up. Did you solve this problem? Thanks in advance!

the code using micro default config is:

if err != nil {
    if err.Error() ==  errs.NoRecord {
        ... // response data not found
    }
}
`

When I shift to grpc, the terrible thing is like what they have described, I have to migrate code like this:

if err != nil {
    if strings.Contains(err.Error(),  errs.NoRecord) {
        ...
    }
}

It's ugly, and I have 8 microservices and have to change all the error handlers one by one.
The problem is not about how to return the errors and instead we need consist apis.

I don't want to rewrite all error handler when I migrate to another rpc framework.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aimpsmile picture aimpsmile  ·  9Comments

nicocesar picture nicocesar  ·  6Comments

ljxqxq picture ljxqxq  ·  10Comments

mkozjak picture mkozjak  ·  9Comments

5teven picture 5teven  ·  5Comments