feat(logging); improve messages and errors (#336)

* feat(logger): add module context

* feat(logger): change errors package

* feat(logger): update tests
This commit is contained in:
Ludvig Lundgren 2022-07-05 13:31:44 +02:00 committed by GitHub
parent 95471a4cf7
commit 0e88117702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
69 changed files with 1172 additions and 957 deletions

View file

@ -3,18 +3,17 @@ package red
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/rs/zerolog/log"
"golang.org/x/time/rate"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"golang.org/x/time/rate"
)
type REDClient interface {
@ -131,8 +130,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
log.Error().Err(err).Msgf("red client request error : %v", url)
return nil, err
return nil, errors.Wrap(err, "could not build request")
}
req.Header.Add("Authorization", c.APIKey)
@ -140,8 +138,7 @@ func (c *Client) get(url string) (*http.Response, error) {
res, err := c.Do(req)
if err != nil {
log.Error().Err(err).Msgf("red client request error : %v", url)
return nil, err
return nil, errors.Wrap(err, "could not make request: %+v", req)
}
if res.StatusCode == http.StatusUnauthorized {
@ -159,7 +156,7 @@ func (c *Client) get(url string) (*http.Response, error) {
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, fmt.Errorf("red client: must have torrentID")
return nil, errors.New("red client: must have torrentID")
}
var r TorrentDetailsResponse
@ -168,23 +165,23 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
v.Add("id", torrentID)
params := v.Encode()
url := fmt.Sprintf("%v?action=torrent&%v", c.URL, params)
reqUrl := fmt.Sprintf("%v?action=torrent&%v", c.URL, params)
resp, err := c.get(url)
resp, err := c.get(reqUrl)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "could not get torrent by id: %v", torrentID)
}
defer resp.Body.Close()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return nil, readErr
return nil, errors.Wrap(readErr, "could not read body")
}
err = json.Unmarshal(body, &r)
if err != nil {
return nil, err
return nil, errors.Wrap(readErr, "could not unmarshal body")
}
return &domain.TorrentBasic{
@ -199,7 +196,7 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
func (c *Client) TestAPI() (bool, error) {
resp, err := c.get(c.URL + "?action=index")
if err != nil {
return false, err
return false, errors.Wrap(err, "could not run test api")
}
defer resp.Body.Close()

View file

@ -1,17 +1,15 @@
package red
import (
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/autobrr/autobrr/internal/domain"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/autobrr/autobrr/internal/domain"
)
func TestREDClient_GetTorrentByID(t *testing.T) {
@ -57,7 +55,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
fields fields
args args
want *domain.TorrentBasic
wantErr error
wantErr string
}{
{
name: "get_by_id_1",
@ -71,7 +69,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
InfoHash: "B2BABD3A361EAFC6C4E9142C422DF7DDF5D7E163",
Size: "527749302",
},
wantErr: nil,
wantErr: "",
},
{
name: "get_by_id_2",
@ -81,7 +79,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
},
args: args{torrentID: "100002"},
want: nil,
wantErr: errors.New("bad id parameter"),
wantErr: "could not get torrent by id: 100002: bad id parameter",
},
{
name: "get_by_id_3",
@ -91,7 +89,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
},
args: args{torrentID: "100002"},
want: nil,
wantErr: errors.New("unauthorized: bad credentials"),
wantErr: "could not get torrent by id: 100002: unauthorized: bad credentials",
},
}
for _, tt := range tests {
@ -99,8 +97,8 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
got, err := c.GetTorrentByID(tt.args.torrentID)
if tt.wantErr != nil && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
if tt.wantErr != "" && assert.Error(t, err) {
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
}
assert.Equal(t, tt.want, got)