How to assert gRPC error codes client side in Go

Myles McDonnell :

Given the following gRPC server side code:

import (
    "google.golang.org/grpc/codes"
    "google.golang.org/grpc/status"
    ....
)

....

func (s *Router) Assign(ctx context.Context, req *api.Request(*api.Response, error) {

    return nil, status.Errorf(codes.PermissionDenied,
}

....

What is the recommended technique for asserting client side that the error is of code = codes.PermissionDenied ?

Varcorb :

Let's say your server returns codes.PermissionDenined like this

...
return nil, status.Error(codes.PermissionDenied, "PERMISSION_DENIED_TEXT")

If your client is Golang as well can also use the status library function FromError to parse the error. I use a switch to determine the error code returned like so

// client
    assignvar, err := s.MyFunctionCall(ctx, ...)
    if err != nil {
        if e, ok := status.FromError(err); ok {
            switch e.Code() {
            case codes.PermissionDenied:
                fmt.Println(e.Message()) // this will print PERMISSION_DENIED_TEST
            case codes.Internal:
                fmt.Println("Has Internal Error")
            case codes.Aborted:
                fmt.Println("gRPC Aborted the call")
            default:
                fmt.Println(e.Code(), e.Message())
            }
        }
        else {
            fmt.Printf("not able to parse error returned %v", err)
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read Meta data in gRPC using Java at client side

gRPC client side load balancing

Interface Python Thrift client with Go gRPC server

how to implement non-blocking client in grpc go?

How does gRPC client-streaming flow control work in go?

How to assert on error types in Go tests?

gRPC context on the client side

How to send grpc meta-data from client side

gRPC: How to get multiple return values in Java client with Go server

How to assert Rest Client Exception?

gRPC: How to configure SSL in client?

Custom Jersey Error Handling, how to catch response at client side?

How to emit error to be possible catch it on error handler on client-side?

Raising a server error to the client with grpc

How to detect an error in item creation on the client side (App Maker)

Java gRPC - TLS - how to set up mutual TLS on the client side?

GRPC how to reuse client connection?

How can a gRPC server notice that the client has cancelled a server-side streaming call?

How to reset Colab after the following CUDA error 'Cuda assert fails: device-side assert triggered'?

How can I receive data on client side before calling .end() on the server side for a gRPC stream

How to confirm gRPC traffic from Go client is TLS encrypted

How to upload an image in chunks with client-side streaming gRPC using grpcurl

How to test if an error occurred on the Server, and output client side

How do I return trigger error back to breezejs client side?

How to describe possible error codes returned by gRPC .proto schema

gRPC, Go & Cloud Run - How to elegantly handle client authorisation?

How to pass status codes to client side? (Redux asyncThunk)

How to catch error of rejected promise on client side?

Server side gRPC service not getting recognised on client Side