Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client hasn't read error msg in rest protocol #392

Merged
merged 1 commit into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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