grpcmap

package
v0.0.0-...-f65aa10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnaryServerInterceptor

func UnaryServerInterceptor(cm map[codes.Code]codes.Code) grpc.UnaryServerInterceptor

UnaryServerInterceptor implements the UnaryServerInterceptor interface. It maps grpc codes from one code to another.

Example
serverAddress := "127.0.0.1:8900"
interceptor := grpc.UnaryInterceptor(
	grpcmap.UnaryServerInterceptor(map[codes.Code]codes.Code{
		codes.Unknown: codes.Internal,
	}),
)
grpcServer := grpc.NewServer(interceptor)
s := TestPingService{}
test.RegisterTestServiceServer(grpcServer, s)
l, _ := net.Listen("tcp", serverAddress)

defer l.Close()
go grpcServer.Serve(l)
defer grpcServer.GracefulStop()

conn, _ := grpc.Dial(serverAddress, grpc.WithInsecure())
c := test.NewTestServiceClient(conn)

req := &test.Request{
	Code:    int32(codes.Internal),
	Message: "an internal error",
}
_, err := c.Ping(context.Background(), req)
fmt.Println(err)

req = &test.Request{
	Code:    int32(codes.Unknown),
	Message: "an unknown error",
}
_, err = c.Ping(context.Background(), req)
fmt.Println(err)

req = &test.Request{
	Code:    int32(codes.NotFound),
	Message: "entity not found",
}
_, err = c.Ping(context.Background(), req)
fmt.Println(err)
Output:

rpc error: code = Internal desc = an internal error
rpc error: code = Internal desc = an unknown error
rpc error: code = NotFound desc = entity not found

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL