This is an automated email from the ASF dual-hosted git repository.

flycash pushed a commit to branch feature/rest
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/feature/rest by this push:
     new b40361e  fix haven't read error msg
     new 182a367  Merge pull request #392 from Patrick0308/rest_protocol
b40361e is described below

commit b40361e9088ee565f0c965a81527fc503d626239
Author: Patrick <dreamlike....@foxmail.com>
AuthorDate: Fri Mar 6 20:29:58 2020 +0800

    fix haven't read error msg
---
 protocol/rest/rest_client/resty_client.go | 16 +++++++++++++---
 protocol/rest/rest_invoker_test.go        | 12 ++++++++++++
 protocol/rest/rest_protocol_test.go       |  5 +++++
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/protocol/rest/rest_client/resty_client.go 
b/protocol/rest/rest_client/resty_client.go
index cdfddcf..88c3cc7 100644
--- a/protocol/rest/rest_client/resty_client.go
+++ b/protocol/rest/rest_client/resty_client.go
@@ -26,6 +26,10 @@ import (
 )
 
 import (
+       perrors "github.com/pkg/errors"
+)
+
+import (
        "github.com/go-resty/resty/v2"
 )
 
@@ -65,16 +69,22 @@ func NewRestyClient(restOption *rest_interface.RestOptions) 
*RestyClient {
 }
 
 func (rc *RestyClient) Do(restRequest *rest_interface.RestRequest, res 
interface{}) error {
-       _, err := rc.client.R().
+       r, err := rc.client.R().
                SetHeader("Content-Type", restRequest.Consumes).
                SetHeader("Accept", restRequest.Produces).
                SetPathParams(restRequest.PathParams).
                SetQueryParams(restRequest.QueryParams).
+               SetHeaders(restRequest.Headers).
                SetBody(restRequest.Body).
                SetResult(res).
-               SetHeaders(restRequest.Headers).
                Execute(restRequest.Method, 
"http://"+path.Join(restRequest.Location, restRequest.Path))
-       return err
+       if err != nil {
+               return perrors.WithStack(err)
+       }
+       if r.IsError() {
+               return perrors.New(r.String())
+       }
+       return nil
 }
 
 func GetRestyClient(restOptions *rest_interface.RestOptions) 
rest_interface.RestClient {
diff --git a/protocol/rest/rest_invoker_test.go 
b/protocol/rest/rest_invoker_test.go
index d2e350e..2ec71b7 100644
--- a/protocol/rest/rest_invoker_test.go
+++ b/protocol/rest/rest_invoker_test.go
@@ -112,6 +112,14 @@ func TestRestInvoker_Invoke(t *testing.T) {
                QueryParamsMap: nil,
                Body:           0,
        }
+       methodConfigMap["GetUserFive"] = &rest_interface.RestMethodConfig{
+               InterfaceName: "",
+               MethodName:    "GetUserFive",
+               Path:          "/GetUserFive",
+               Produces:      "*/*",
+               Consumes:      "*/*",
+               MethodType:    "GET",
+       }
        methodConfigMap["GetUser"] = &rest_interface.RestMethodConfig{
                InterfaceName:  "",
                MethodName:     "GetUser",
@@ -175,6 +183,10 @@ func TestRestInvoker_Invoke(t *testing.T) {
        assert.NoError(t, res.Error())
        assert.NotNil(t, res.Result())
        assert.Equal(t, "username", res.Result().(*User).Name)
+       inv = 
invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFive"),
 invocation.WithReply(user))
+       res = invoker.Invoke(context.Background(), inv)
+       assert.Error(t, res.Error(), "test error")
+
        err = common.ServiceMap.UnRegister(url.Protocol, 
"com.ikurento.user.UserProvider")
        assert.NoError(t, err)
 }
diff --git a/protocol/rest/rest_protocol_test.go 
b/protocol/rest/rest_protocol_test.go
index 0c3628d..28250d0 100644
--- a/protocol/rest/rest_protocol_test.go
+++ b/protocol/rest/rest_protocol_test.go
@@ -19,6 +19,7 @@ package rest
 
 import (
        "context"
+       "errors"
        "fmt"
        "strings"
        "testing"
@@ -173,6 +174,10 @@ func (p *UserProvider) GetUserFour(ctx context.Context, 
user []interface{}, id s
        return u, nil
 }
 
+func (p *UserProvider) GetUserFive(ctx context.Context, user []interface{}) 
(*User, error) {
+       return nil, errors.New("test error")
+}
+
 type User struct {
        Id   int
        Time *time.Time

Reply via email to