test: add tests for errors

This commit is contained in:
mehanizm
2020-04-17 23:12:07 +03:00
parent bc37bb1eb4
commit 4630f5f396
4 changed files with 38 additions and 7 deletions

View File

@@ -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)

View File

@@ -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")
}
}

View File

@@ -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
View 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)
}
}