grpc client/server fixes (#1355)
* grpc client/server fixes Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
b722b4338b
commit
422f951014
19
grpc.go
19
grpc.go
@ -279,6 +279,9 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) error {
|
|||||||
|
|
||||||
// serve the actual request using the request router
|
// serve the actual request using the request router
|
||||||
if err := r.ServeRequest(ctx, request, response); err != nil {
|
if err := r.ServeRequest(ctx, request, response); err != nil {
|
||||||
|
if _, ok := status.FromError(err); ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return status.Errorf(codes.Internal, err.Error())
|
return status.Errorf(codes.Internal, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,7 +382,6 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service,
|
|||||||
for i := len(g.opts.HdlrWrappers); i > 0; i-- {
|
for i := len(g.opts.HdlrWrappers); i > 0; i-- {
|
||||||
fn = g.opts.HdlrWrappers[i-1](fn)
|
fn = g.opts.HdlrWrappers[i-1](fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
statusCode := codes.OK
|
statusCode := codes.OK
|
||||||
statusDesc := ""
|
statusDesc := ""
|
||||||
// execute the handler
|
// execute the handler
|
||||||
@ -402,24 +404,19 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case *rpcError:
|
|
||||||
// rpcError handling may be we have ability to attach it to details?
|
|
||||||
statusCode = verr.code
|
|
||||||
statusDesc = verr.desc
|
|
||||||
errStatus = status.New(statusCode, statusDesc)
|
|
||||||
default:
|
default:
|
||||||
// default case user pass own error type that not proto based
|
// default case user pass own error type that not proto based
|
||||||
statusCode = convertCode(verr)
|
statusCode = convertCode(verr)
|
||||||
statusDesc = verr.Error()
|
statusDesc = verr.Error()
|
||||||
errStatus = status.New(statusCode, statusDesc)
|
errStatus = status.New(statusCode, statusDesc)
|
||||||
}
|
}
|
||||||
|
|
||||||
return errStatus.Err()
|
return errStatus.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stream.SendMsg(replyv.Interface()); err != nil {
|
if err := stream.SendMsg(replyv.Interface()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return status.New(statusCode, statusDesc).Err()
|
return status.New(statusCode, statusDesc).Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -459,8 +456,7 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m
|
|||||||
statusCode := codes.OK
|
statusCode := codes.OK
|
||||||
statusDesc := ""
|
statusDesc := ""
|
||||||
|
|
||||||
appErr := fn(ctx, r, ss)
|
if appErr := fn(ctx, r, ss); appErr != nil {
|
||||||
if appErr != nil {
|
|
||||||
var err error
|
var err error
|
||||||
var errStatus *status.Status
|
var errStatus *status.Status
|
||||||
switch verr := appErr.(type) {
|
switch verr := appErr.(type) {
|
||||||
@ -480,11 +476,6 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case *rpcError:
|
|
||||||
// rpcError handling may be we have ability to attach it to details?
|
|
||||||
statusCode = verr.code
|
|
||||||
statusDesc = verr.desc
|
|
||||||
errStatus = status.New(statusCode, statusDesc)
|
|
||||||
default:
|
default:
|
||||||
// default case user pass own error type that not proto based
|
// default case user pass own error type that not proto based
|
||||||
statusCode = convertCode(verr)
|
statusCode = convertCode(verr)
|
||||||
|
11
util.go
11
util.go
@ -2,7 +2,6 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@ -10,16 +9,6 @@ import (
|
|||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rpcError defines the status from an RPC.
|
|
||||||
type rpcError struct {
|
|
||||||
code codes.Code
|
|
||||||
desc string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rpcError) Error() string {
|
|
||||||
return fmt.Sprintf("rpc error: code = %d desc = %s", e.code, e.desc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertCode converts a standard Go error into its canonical code. Note that
|
// convertCode converts a standard Go error into its canonical code. Note that
|
||||||
// this is only used to translate the error returned by the server applications.
|
// this is only used to translate the error returned by the server applications.
|
||||||
func convertCode(err error) codes.Code {
|
func convertCode(err error) codes.Code {
|
||||||
|
Loading…
Reference in New Issue
Block a user