From b40361e9088ee565f0c965a81527fc503d626239 Mon Sep 17 00:00:00 2001
From: Patrick <dreamlike.sky@foxmail.com>
Date: Fri, 6 Mar 2020 20:29:58 +0800
Subject: [PATCH] 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 cdfddcfbe..88c3cc77e 100644
--- a/protocol/rest/rest_client/resty_client.go
+++ b/protocol/rest/rest_client/resty_client.go
@@ -25,6 +25,10 @@ import (
 	"time"
 )
 
+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 d2e350e30..2ec71b775 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 0c3628d7c..28250d092 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
-- 
GitLab