Skip to content

Commit

Permalink
chore(response): optimize ErrorResponse (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Aug 28, 2024
1 parent a862e56 commit 89ad692
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
9 changes: 4 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -185,18 +184,18 @@ func (c *Client) Do(req *http.Request, v any) (*Response, error) {
defer resp.Body.Close() // nolint:errcheck
defer io.Copy(io.Discard, resp.Body) // nolint:errcheck

// todo: if status code is not 200

// decode response body
var rawBody RawBody
if err := json.NewDecoder(resp.Body).Decode(&rawBody); err != nil {
return nil, err
}

// debug mode
body, _ := json.Marshal(rawBody)
fmt.Println(string(body))
// body, _ := json.Marshal(rawBody)
// fmt.Println(string(body))
// spew.Dump(rawBody)

// check status
if rawBody.Status != 1 {
return nil, &ErrorResponse{
response: resp,
Expand Down
20 changes: 20 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ func TestClient_BasicAuth(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestClient_ErrorResponse(t *testing.T) {
_, client := createServerClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, "/__/error-response", r.URL.Path)

fmt.Fprint(w, `{
"status": 429,
"data": {},
"info": "To many requests, api account brookechen max request rates is 6000req/10min"
}`)
}))

req, err := client.NewRequest(ctx, http.MethodGet, "__/error-response", nil, nil)
assert.NoError(t, err)

_, err = client.Do(req, nil)
assert.Error(t, err)
assert.True(t, IsErrorResponse(err))
}
10 changes: 9 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tapd

import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
Expand All @@ -11,17 +12,19 @@ type Response struct {
*http.Response
}

// newResponse creates a new Response.
func newResponse(httpResp *http.Response) *Response {
return &Response{Response: httpResp}
}

// RawBody represents a raw body.
type RawBody struct {
Status int `json:"status"`
Data json.RawMessage `json:"data"`
Info string `json:"info"`
}

// ErrorResponse represents an error response.
// ErrorResponse represents a tapd error response.
type ErrorResponse struct {
response *http.Response
rawBody *RawBody
Expand All @@ -44,6 +47,11 @@ func (e *ErrorResponse) Unwrap() error {
return e.err
}

func IsErrorResponse(err error) bool {
var e *ErrorResponse
return errors.As(err, &e)
}

// CountResponse represents the response of count.
type CountResponse struct {
Count int `json:"count"`
Expand Down
33 changes: 33 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tapd

import (
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestResponse_ErrorResponse(t *testing.T) {
tests := []struct {
name string
err error
want string
unwrapErr error
isErrorResponse bool
}{
{"error", errors.New("error"), "error", nil, false}, //nolint:lll
{"error response", &ErrorResponse{err: errors.New("error")}, "error", errors.New("error"), true}, //nolint:lll
{"error response with raw body", &ErrorResponse{rawBody: &RawBody{Status: 1, Info: "info"}}, "code: 1, info: info", nil, true}, //nolint:lll
{"error response with http response", &ErrorResponse{response: &http.Response{StatusCode: 404}, err: errors.New("error")}, "status code: 404, err: error", errors.New("error"), true}, //nolint:lll
{"error response with http response and raw body", &ErrorResponse{response: &http.Response{StatusCode: 404}, rawBody: &RawBody{Status: 1, Info: "info"}}, "code: 1, info: info", nil, true}, //nolint:lll
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.err.Error())
assert.Equal(t, tt.unwrapErr, errors.Unwrap(tt.err))
assert.Equal(t, tt.isErrorResponse, IsErrorResponse(tt.err))
})
}
}

0 comments on commit 89ad692

Please sign in to comment.