Skip to content

Commit

Permalink
Fixed client error handling and README.md example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaksv committed Oct 6, 2021
1 parent bb79083 commit 59c0d8f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func main() {
CountryCode: "RU",
})
if err != nil {
if serviceErr, ok := err.(*apidq.ErrorResponse); ok {
fmt.Printf("Code: %d; Message: %s", serviceErr.Code, serviceErr.Message)
}
panic(err)
}
}

fmt.Println(cleanRsp.Address.Address) // -- print: г Москва, пл Спартаковская
}
Expand Down
11 changes: 8 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/url"
"strings"
"time"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -180,18 +182,21 @@ func (c *Client) do(_ context.Context, req *http.Request, v interface{}) (rsp *h
if rsp.StatusCode != 200 {
errRsp := &ErrorResponse{}
decErr := json.Unmarshal(body, errRsp)
if decErr == nil {
if decErr == nil && errRsp.Code != 0 && errRsp.Message != "" {
return nil, errRsp
}
return nil, decErr
if decErr == nil {
decErr = errors.New("invalid ErrorResponse struct")
}
return nil, errors.WithMessage(decErr, string(body))
}

decErr := json.Unmarshal(body, v)
if decErr == nil {
return rsp, nil
}

return nil, decErr
return nil, errors.WithMessage(decErr, string(body))
}

return rsp, nil
Expand Down
77 changes: 77 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestClientHost(t *testing.T) {
c, err := NewClient(http.DefaultClient, "http://no-connect-url.local:9999")
if err != nil {
panic(err)
}

_, _, err = c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need network error"))
}
if !strings.Contains(err.Error(), "dial tcp: lookup no-connect-url.local: no such host") {
t.Fatal(err.Error())
}
}

func TestClientIncorrectError(t *testing.T) {
invalidErrRsp := []byte(`{"code":"invalid code type","message":false}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
_, err := w.Write(invalidErrRsp)
if err != nil {
panic(err)
}
})

defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need ErrorResponse"))
}
if err.Error() != "{\"code\":\"invalid code type\",\"message\":false}: json: cannot unmarshal string into Go struct field ErrorResponse.code of type int" {
t.Fatal(err)
}
}
func TestClientEmptyErrorResponse(t *testing.T) {
invalidErrRsp := []byte(`{"asd":"qwe"}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
_, err := w.Write(invalidErrRsp)
if err != nil {
panic(err)
}
})

defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need error"))
}

if err.Error() != "{\"asd\":\"qwe\"}: invalid ErrorResponse struct" {
t.Fatal(err)
}
}

func TestClientIncorrectResponse(t *testing.T) {
invalidRsp := []byte(`{"origin":"123","area":false}`)
c, s := NewTestClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write(invalidRsp)
if err != nil {
panic(err)
}
})

defer s.Close()
_, _, err := c.Address.Clean(context.Background(), &address.CleanRequest{})
if err == nil {
t.Fatal(errors.New("need error"))
}
if err.Error() != "{\"origin\":\"123\",\"area\":false}: json: cannot unmarshal bool into Go struct field CleanResponse.area of type address.Part" {
t.Fatal(err)
}

}

func TestAuth(t *testing.T) {
client, tS := NewTestClient(func(w http.ResponseWriter, r *http.Request) {})
defer tS.Close()
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/nikitaksv/apidq-client-go

go 1.15

require github.com/stretchr/testify v1.7.0
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 59c0d8f

Please sign in to comment.