mirror of https://github.com/grpc/grpc-go.git
Make Errorf return pointer to rpcError
This commit is contained in:
parent
461dac9997
commit
3802318f46
|
@ -234,7 +234,7 @@ func TestInvokeLargeErr(t *testing.T) {
|
|||
var reply string
|
||||
req := "hello"
|
||||
err := Invoke(context.Background(), "/foo/bar", &req, &reply, cc)
|
||||
if _, ok := err.(rpcError); !ok {
|
||||
if _, ok := err.(*rpcError); !ok {
|
||||
t.Fatalf("grpc.Invoke(_, _, _, _, _) receives non rpc error.")
|
||||
}
|
||||
if Code(err) != codes.Internal || len(ErrorDesc(err)) != sizeLargeErr {
|
||||
|
@ -250,7 +250,7 @@ func TestInvokeErrorSpecialChars(t *testing.T) {
|
|||
var reply string
|
||||
req := "weird error"
|
||||
err := Invoke(context.Background(), "/foo/bar", &req, &reply, cc)
|
||||
if _, ok := err.(rpcError); !ok {
|
||||
if _, ok := err.(*rpcError); !ok {
|
||||
t.Fatalf("grpc.Invoke(_, _, _, _, _) receives non rpc error.")
|
||||
}
|
||||
if got, want := ErrorDesc(err), weirdError; got != want {
|
||||
|
|
14
rpc_util.go
14
rpc_util.go
|
@ -334,7 +334,7 @@ type rpcError struct {
|
|||
desc string
|
||||
}
|
||||
|
||||
func (e rpcError) Error() string {
|
||||
func (e *rpcError) Error() string {
|
||||
return fmt.Sprintf("rpc error: code = %d desc = %s", e.code, e.desc)
|
||||
}
|
||||
|
||||
|
@ -344,7 +344,7 @@ func Code(err error) codes.Code {
|
|||
if err == nil {
|
||||
return codes.OK
|
||||
}
|
||||
if e, ok := err.(rpcError); ok {
|
||||
if e, ok := err.(*rpcError); ok {
|
||||
return e.code
|
||||
}
|
||||
return codes.Unknown
|
||||
|
@ -356,7 +356,7 @@ func ErrorDesc(err error) string {
|
|||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if e, ok := err.(rpcError); ok {
|
||||
if e, ok := err.(*rpcError); ok {
|
||||
return e.desc
|
||||
}
|
||||
return err.Error()
|
||||
|
@ -368,7 +368,7 @@ func Errorf(c codes.Code, format string, a ...interface{}) error {
|
|||
if c == codes.OK {
|
||||
return nil
|
||||
}
|
||||
return rpcError{
|
||||
return &rpcError{
|
||||
code: c,
|
||||
desc: fmt.Sprintf(format, a...),
|
||||
}
|
||||
|
@ -377,15 +377,15 @@ func Errorf(c codes.Code, format string, a ...interface{}) error {
|
|||
// toRPCErr converts an error into a rpcError.
|
||||
func toRPCErr(err error) error {
|
||||
switch e := err.(type) {
|
||||
case rpcError:
|
||||
case *rpcError:
|
||||
return err
|
||||
case transport.StreamError:
|
||||
return rpcError{
|
||||
return &rpcError{
|
||||
code: e.Code,
|
||||
desc: e.Desc,
|
||||
}
|
||||
case transport.ConnectionError:
|
||||
return rpcError{
|
||||
return &rpcError{
|
||||
code: codes.Internal,
|
||||
desc: e.Desc,
|
||||
}
|
||||
|
|
|
@ -149,13 +149,17 @@ func TestToRPCErr(t *testing.T) {
|
|||
// input
|
||||
errIn error
|
||||
// outputs
|
||||
errOut error
|
||||
errOut *rpcError
|
||||
}{
|
||||
{transport.StreamErrorf(codes.Unknown, ""), Errorf(codes.Unknown, "")},
|
||||
{transport.ErrConnClosing, Errorf(codes.Internal, transport.ErrConnClosing.Desc)},
|
||||
{transport.StreamErrorf(codes.Unknown, ""), Errorf(codes.Unknown, "").(*rpcError)},
|
||||
{transport.ErrConnClosing, Errorf(codes.Internal, transport.ErrConnClosing.Desc).(*rpcError)},
|
||||
} {
|
||||
err := toRPCErr(test.errIn)
|
||||
if err != test.errOut {
|
||||
rpcErr, ok := err.(*rpcError)
|
||||
if !ok {
|
||||
t.Fatalf("toRPCErr{%v} returned type %T, want %T", test.errIn, err, rpcError{})
|
||||
}
|
||||
if *rpcErr != *test.errOut {
|
||||
t.Fatalf("toRPCErr{%v} = %v \nwant %v", test.errIn, err, test.errOut)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -560,7 +560,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
|
|||
}
|
||||
reply, appErr := md.Handler(srv.server, stream.Context(), df, s.opts.unaryInt)
|
||||
if appErr != nil {
|
||||
if err, ok := appErr.(rpcError); ok {
|
||||
if err, ok := appErr.(*rpcError); ok {
|
||||
statusCode = err.code
|
||||
statusDesc = err.desc
|
||||
} else {
|
||||
|
@ -645,7 +645,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
|
|||
appErr = s.opts.streamInt(srv.server, ss, info, sd.Handler)
|
||||
}
|
||||
if appErr != nil {
|
||||
if err, ok := appErr.(rpcError); ok {
|
||||
if err, ok := appErr.(*rpcError); ok {
|
||||
ss.statusCode = err.code
|
||||
ss.statusDesc = err.desc
|
||||
} else if err, ok := appErr.(transport.StreamError); ok {
|
||||
|
|
Loading…
Reference in New Issue