test: add tests for errors
This commit is contained in:
15
client.go
15
client.go
@@ -8,6 +8,7 @@ package airtable
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@@ -64,7 +65,7 @@ func (at *Client) get(db, table, recordID string, params url.Values, target inte
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
|
||||
req.URL.RawQuery = params.Encode()
|
||||
err = at.do(req, url, target)
|
||||
err = at.do(req, target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -84,7 +85,7 @@ func (at *Client) post(db, table string, data, response interface{}) error {
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
|
||||
return at.do(req, url, response)
|
||||
return at.do(req, response)
|
||||
}
|
||||
|
||||
func (at *Client) delete(db, table string, recordIDs []string, target interface{}) error {
|
||||
@@ -101,7 +102,7 @@ func (at *Client) delete(db, table string, recordIDs []string, target interface{
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
|
||||
req.URL.RawQuery = params.Encode()
|
||||
err = at.do(req, rawURL, target)
|
||||
err = at.do(req, target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -121,10 +122,14 @@ func (at *Client) patch(db, table, data, response interface{}) error {
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", at.apiKey))
|
||||
return at.do(req, url, response)
|
||||
return at.do(req, response)
|
||||
}
|
||||
|
||||
func (at *Client) do(req *http.Request, url string, response interface{}) error {
|
||||
func (at *Client) do(req *http.Request, response interface{}) error {
|
||||
if req == nil {
|
||||
return errors.New("nil request")
|
||||
}
|
||||
url := req.URL.RequestURI()
|
||||
resp, err := at.client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("HTTP request failure on %s: %w", url, err)
|
||||
|
||||
@@ -25,9 +25,13 @@ func TestClient_do(t *testing.T) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = c.do(req, url, nil)
|
||||
err = c.do(req, nil)
|
||||
var e *HTTPClientError
|
||||
if errors.Is(err, e) {
|
||||
t.Errorf("should be an http error, but was not: %v", err)
|
||||
}
|
||||
err = c.do(nil, nil)
|
||||
if err == nil {
|
||||
t.Errorf("there should be an error, but was nil")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type HTTPClientError struct {
|
||||
}
|
||||
|
||||
func (e *HTTPClientError) Error() string {
|
||||
return fmt.Sprintf("status %d: err %v", e.StatusCode, e.Err)
|
||||
return fmt.Sprintf("status %d, err: %v", e.StatusCode, e.Err)
|
||||
}
|
||||
|
||||
func makeHTTPClientError(url string, resp *http.Response) error {
|
||||
|
||||
22
errors_test.go
Normal file
22
errors_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright © 2020 Mike Berezin
|
||||
//
|
||||
// Use of this source code is governed by an MIT license.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package airtable
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTPClientError_Error(t *testing.T) {
|
||||
e := &HTTPClientError{
|
||||
StatusCode: 300,
|
||||
Err: errors.New("error message"),
|
||||
}
|
||||
expected := "status 300, err: error message"
|
||||
if got := e.Error(); got != expected {
|
||||
t.Errorf("HTTPClientError.Error() = %v, want %v", got, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user