Skip to content

Commit 59c0d8f

Browse files
committed
Fixed client error handling and README.md example usage
1 parent bb79083 commit 59c0d8f

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ func main() {
5353
CountryCode: "RU",
5454
})
5555
if err != nil {
56+
if serviceErr, ok := err.(*apidq.ErrorResponse); ok {
57+
fmt.Printf("Code: %d; Message: %s", serviceErr.Code, serviceErr.Message)
58+
}
5659
panic(err)
57-
}
60+
}
5861

5962
fmt.Println(cleanRsp.Address.Address) // -- print: г Москва, пл Спартаковская
6063
}

client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"net/url"
1212
"strings"
1313
"time"
14+
15+
"github.com/pkg/errors"
1416
)
1517

1618
const (
@@ -180,18 +182,21 @@ func (c *Client) do(_ context.Context, req *http.Request, v interface{}) (rsp *h
180182
if rsp.StatusCode != 200 {
181183
errRsp := &ErrorResponse{}
182184
decErr := json.Unmarshal(body, errRsp)
183-
if decErr == nil {
185+
if decErr == nil && errRsp.Code != 0 && errRsp.Message != "" {
184186
return nil, errRsp
185187
}
186-
return nil, decErr
188+
if decErr == nil {
189+
decErr = errors.New("invalid ErrorResponse struct")
190+
}
191+
return nil, errors.WithMessage(decErr, string(body))
187192
}
188193

189194
decErr := json.Unmarshal(body, v)
190195
if decErr == nil {
191196
return rsp, nil
192197
}
193198

194-
return nil, decErr
199+
return nil, errors.WithMessage(decErr, string(body))
195200
}
196201

197202
return rsp, nil

client_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"net/http"
99
"net/http/httptest"
10+
"strings"
1011
"testing"
1112

1213
"github.com/nikitaksv/apidq-client-go/dto/address"
@@ -44,6 +45,82 @@ func NewTestClient(h http.HandlerFunc) (*Client, *httptest.Server) {
4445
return c.WithAuth(TestAPIKey), s
4546
}
4647

48+
func TestClientHost(t *testing.T) {
49+
c, err := NewClient(http.DefaultClient, "http://no-connect-url.local:9999")
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
_, _, err = c.Address.Clean(context.Background(), &address.CleanRequest{})
55+
if err == nil {
56+
t.Fatal(errors.New("need network error"))
57+
}
58+
if !strings.Contains(err.Error(), "dial tcp: lookup no-connect-url.local: no such host") {
59+
t.Fatal(err.Error())
60+
}
61+
}
62+
63+
func TestClientIncorrectError(t *testing.T) {
64+
invalidErrRsp := []byte(`{"code":"invalid code type","message":false}`)
65+
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
66+
w.WriteHeader(400)
67+
_, err := w.Write(invalidErrRsp)
68+
if err != nil {
69+
panic(err)
70+
}
71+
})
72+
73+
defer s.Close()
74+
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
75+
if err == nil {
76+
t.Fatal(errors.New("need ErrorResponse"))
77+
}
78+
if err.Error() != "{\"code\":\"invalid code type\",\"message\":false}: json: cannot unmarshal string into Go struct field ErrorResponse.code of type int" {
79+
t.Fatal(err)
80+
}
81+
}
82+
func TestClientEmptyErrorResponse(t *testing.T) {
83+
invalidErrRsp := []byte(`{"asd":"qwe"}`)
84+
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
85+
w.WriteHeader(400)
86+
_, err := w.Write(invalidErrRsp)
87+
if err != nil {
88+
panic(err)
89+
}
90+
})
91+
92+
defer s.Close()
93+
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
94+
if err == nil {
95+
t.Fatal(errors.New("need error"))
96+
}
97+
98+
if err.Error() != "{\"asd\":\"qwe\"}: invalid ErrorResponse struct" {
99+
t.Fatal(err)
100+
}
101+
}
102+
103+
func TestClientIncorrectResponse(t *testing.T) {
104+
invalidRsp := []byte(`{"origin":"123","area":false}`)
105+
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
106+
w.WriteHeader(200)
107+
_, err := w.Write(invalidRsp)
108+
if err != nil {
109+
panic(err)
110+
}
111+
})
112+
113+
defer s.Close()
114+
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
115+
if err == nil {
116+
t.Fatal(errors.New("need error"))
117+
}
118+
if err.Error() != "{\"origin\":\"123\",\"area\":false}: json: cannot unmarshal bool into Go struct field CleanResponse.area of type address.Part" {
119+
t.Fatal(err)
120+
}
121+
122+
}
123+
47124
func TestAuth(t *testing.T) {
48125
client, tS := NewTestClient(func(w http.ResponseWriter, r *http.Request) {})
49126
defer tS.Close()

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/nikitaksv/apidq-client-go
22

33
go 1.15
44

5-
require github.com/stretchr/testify v1.7.0
5+
require (
6+
github.com/pkg/errors v0.9.1
7+
github.com/stretchr/testify v1.7.0
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

0 commit comments

Comments
 (0)