Skip to content

Commit

Permalink
Merge pull request #392 from Patrick0308/rest_protocol
Browse files Browse the repository at this point in the history
Fix client hasn't read error msg in rest protocol
  • Loading branch information
flycash authored Mar 8, 2020
2 parents 4b01d83 + b40361e commit 182a367
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 13 additions & 3 deletions protocol/rest/rest_client/resty_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"time"
)

import (
perrors "github.com/pkg/errors"
)

import (
"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions protocol/rest/rest_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions protocol/rest/rest_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package rest

import (
"context"
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 182a367

Please sign in to comment.