fix error decode (#1068)

* fix error decode
pull/1071/head
longxboy 3 years ago committed by GitHub
parent 4e96e08471
commit db02034dd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      transport/http/client.go
  2. 5
      transport/http/client_test.go

@ -298,6 +298,7 @@ func DefaultErrorDecoder(ctx context.Context, res *http.Response) error {
if err == nil { if err == nil {
e := new(errors.Error) e := new(errors.Error)
if err = CodecForResponse(res).Unmarshal(data, e); err == nil { if err = CodecForResponse(res).Unmarshal(data, e); err == nil {
e.Code = int32(res.StatusCode)
return e return e
} }
} }

@ -4,12 +4,13 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"github.com/go-kratos/kratos/v2/errors"
"io/ioutil" "io/ioutil"
nethttp "net/http" nethttp "net/http"
"testing" "testing"
"time" "time"
"github.com/go-kratos/kratos/v2/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
@ -161,7 +162,7 @@ func TestDefaultErrorDecoder(t *testing.T) {
} }
err2 := DefaultErrorDecoder(context.TODO(), resp2) err2 := DefaultErrorDecoder(context.TODO(), resp2)
assert.Error(t, err2) 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, "hi", err2.(*errors.Error).GetMessage())
assert.Equal(t, "FOO", err2.(*errors.Error).GetReason()) assert.Equal(t, "FOO", err2.(*errors.Error).GetReason())
} }

Loading…
Cancel
Save