From db02034dd16da5650858d1957e40085543c4019f Mon Sep 17 00:00:00 2001 From: longxboy Date: Wed, 16 Jun 2021 19:54:19 +0800 Subject: [PATCH] fix error decode (#1068) * fix error decode --- transport/http/client.go | 1 + transport/http/client_test.go | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/transport/http/client.go b/transport/http/client.go index 9eb84c51a..a21557e90 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -298,6 +298,7 @@ func DefaultErrorDecoder(ctx context.Context, res *http.Response) error { if err == nil { e := new(errors.Error) if err = CodecForResponse(res).Unmarshal(data, e); err == nil { + e.Code = int32(res.StatusCode) return e } } diff --git a/transport/http/client_test.go b/transport/http/client_test.go index 30e4ba3b5..22b5d835c 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -4,12 +4,13 @@ import ( "bytes" "context" "encoding/json" - "github.com/go-kratos/kratos/v2/errors" "io/ioutil" nethttp "net/http" "testing" "time" + "github.com/go-kratos/kratos/v2/errors" + "github.com/stretchr/testify/assert" "github.com/go-kratos/kratos/v2/registry" @@ -161,7 +162,7 @@ func TestDefaultErrorDecoder(t *testing.T) { } err2 := DefaultErrorDecoder(context.TODO(), resp2) assert.Error(t, err2) - assert.Equal(t, int32(54321), err2.(*errors.Error).GetCode()) + assert.Equal(t, int32(500), err2.(*errors.Error).GetCode()) assert.Equal(t, "hi", err2.(*errors.Error).GetMessage()) assert.Equal(t, "FOO", err2.(*errors.Error).GetReason()) }