Skip to content

Commit

Permalink
feat: return rpc detail error message (#811)
Browse files Browse the repository at this point in the history
Co-authored-by: 文徐 <[email protected]>
  • Loading branch information
wenxuwan and wenxuwan authored Oct 14, 2022
1 parent 34bacab commit 0629c4e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
3 changes: 2 additions & 1 deletion components/rpc/invoker/mosn/mosninvoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (m *mosnInvoker) Invoke(ctx context.Context, req *rpc.RPCRequest) (resp *rp
resp, err = m.cb.AfterInvoke(resp)
if err != nil {
log.DefaultLogger.Errorf("[runtime][rpc]after filter error %s", err.Error())
return nil, err
}
return resp, err
return resp, nil
}
18 changes: 11 additions & 7 deletions components/rpc/invoker/mosn/transport_protocol/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ func (b *boltCommon) Init(conf map[string]interface{}) error {
// FromFrame is boltProtocol transform
func (b *boltCommon) FromFrame(resp api.XRespFrame) (*rpc.RPCResponse, error) {
respCode := uint16(resp.GetStatusCode())
if respCode == bolt.ResponseStatusSuccess {
return b.fromFrame.FromFrame(resp)
rpcResp, err := b.fromFrame.FromFrame(resp)
rpcResp.Success = false
if err != nil {
return nil, common.Errorf(common.InternalCode, "bolt error code %d, transform RPCResponse fail with err: %+v", respCode, err)
}

switch respCode {
case bolt.ResponseStatusSuccess:
rpcResp.Success = true
case bolt.ResponseStatusServerDeserialException:
return nil, common.Errorf(common.InternalCode, "bolt error code %d, ServerDeserializeException", respCode)
rpcResp.Error = common.Errorf(common.InternalCode, "bolt error code %d, ServerDeserializeException", respCode)
case bolt.ResponseStatusServerSerialException:
return nil, common.Errorf(common.InternalCode, "bolt error code %d, ServerSerializeException", respCode)
rpcResp.Error = common.Errorf(common.InternalCode, "bolt error code %d, ServerSerializeException", respCode)
case bolt.ResponseStatusCodecException:
return nil, common.Errorf(common.InternalCode, "bolt error code %d, CodecException", respCode)
rpcResp.Error = common.Errorf(common.InternalCode, "bolt error code %d, CodecException", respCode)
default:
return nil, common.Errorf(common.UnavailebleCode, "bolt error code %d", respCode)
rpcResp.Error = common.Errorf(common.UnavailebleCode, "bolt error code %d", respCode)
}
return rpcResp, nil
}

// newBoltProtocol is create boltProtocol
Expand Down
55 changes: 52 additions & 3 deletions components/rpc/invoker/mosn/transport_protocol/bolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,58 @@ func Test_boltCommon_FromFrame(t *testing.T) {
err := b.Init(conf)
assert.Nil(t, err)

_, err = b.FromFrame(resp)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "bolt error code 1"))
f, err := b.FromFrame(resp)
assert.Nil(t, err)
assert.Equal(t, false, f.Success)
assert.True(t, strings.Contains(f.Error.Error(), "bolt error code 1"))
})

t.Run("fail", func(t *testing.T) {
resp := &bolt.Response{}
resp.ResponseStatus = bolt.ResponseStatusServerDeserialException
b := &boltCommon{}
conf := map[string]interface{}{
"class": "bolt",
}
err := b.Init(conf)
assert.Nil(t, err)

f, err := b.FromFrame(resp)
assert.Nil(t, err)
assert.Equal(t, false, f.Success)
assert.True(t, strings.Contains(f.Error.Error(), "bolt error code 18"))
})

t.Run("fail", func(t *testing.T) {
resp := &bolt.Response{}
resp.ResponseStatus = bolt.ResponseStatusServerSerialException
b := &boltCommon{}
conf := map[string]interface{}{
"class": "bolt",
}
err := b.Init(conf)
assert.Nil(t, err)

f, err := b.FromFrame(resp)
assert.Nil(t, err)
assert.Equal(t, false, f.Success)
assert.True(t, strings.Contains(f.Error.Error(), "bolt error code 17"))
})

t.Run("fail", func(t *testing.T) {
resp := &bolt.Response{}
resp.ResponseStatus = bolt.ResponseStatusCodecException
b := &boltCommon{}
conf := map[string]interface{}{
"class": "bolt",
}
err := b.Init(conf)
assert.Nil(t, err)

f, err := b.FromFrame(resp)
assert.Nil(t, err)
assert.Equal(t, false, f.Success)
assert.True(t, strings.Contains(f.Error.Error(), "bolt error code 9"))
})
}

Expand Down
2 changes: 2 additions & 0 deletions components/rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type RPCResponse struct {
Header RPCHeader
ContentType string
Data []byte
Success bool
Error error
}

type RpcConfig struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/grpc/dapr/dapr_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (d *daprGrpcAPI) InvokeService(ctx context.Context, in *dapr_v1pb.InvokeSer
if err != nil {
return nil, runtime_common.ToGrpcError(err)
}
// 5. convert result
if !resp.Success && resp.Error != nil {
return nil, runtime_common.ToGrpcError(resp.Error)
}

if resp.Header != nil {
header := metadata.Pairs()
Expand Down

0 comments on commit 0629c4e

Please sign in to comment.