mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
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:
parent
95471a4cf7
commit
0e88117702
69 changed files with 1172 additions and 957 deletions
|
@ -3,14 +3,12 @@ package radarr
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
|
@ -20,8 +18,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client request error : %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -32,15 +29,14 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client.get request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("radarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "radarr.http.Do(req): %v", reqUrl)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("radarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "radarr.io.Copy")
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
|
@ -53,14 +49,12 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client could not marshal data: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -73,19 +67,15 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
log.Error().Err(err).Msgf("radarr client bad request: %v", reqUrl)
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode == http.StatusBadRequest {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return nil, errors.New("radarr: bad request")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return nil, errors.New("radarr: bad request")
|
||||
}
|
||||
|
||||
|
@ -100,14 +90,12 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client could not marshal data: %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.Wrap(err, "could not build request: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -118,19 +106,20 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("radarr client request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("radarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "radarr.http.Do(req): %+v", req)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("radarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "radarr.io.Copy")
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return resp.StatusCode, buf.Bytes(), fmt.Errorf("radarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
if resp.StatusCode == http.StatusBadRequest {
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
} else if resp.StatusCode < 200 || resp.StatusCode > 401 {
|
||||
return resp.StatusCode, buf.Bytes(), errors.New("radarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
|
|
|
@ -2,12 +2,14 @@ package radarr
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -18,6 +20,8 @@ type Config struct {
|
|||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
|
@ -28,6 +32,8 @@ type Client interface {
|
|||
type client struct {
|
||||
config Config
|
||||
http *http.Client
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
func New(config Config) Client {
|
||||
|
@ -39,6 +45,11 @@ func New(config Config) Client {
|
|||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
|
||||
return c
|
||||
|
@ -65,11 +76,17 @@ type SystemStatusResponse struct {
|
|||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type BadRequestResponse struct {
|
||||
PropertyName string `json:"propertyName"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
AttemptedValue string `json:"attemptedValue"`
|
||||
Severity string `json:"severity"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("radarr client get error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "radarr error running test")
|
||||
}
|
||||
|
||||
if status == http.StatusUnauthorized {
|
||||
|
@ -79,11 +96,10 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("radarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
log.Trace().Msgf("radarr system/status response: %+v", response)
|
||||
c.Log.Printf("radarr system/status status: (%v) response: %v\n", status, string(res))
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
@ -91,24 +107,35 @@ func (c *client) Test() (*SystemStatusResponse, error) {
|
|||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("radarr client post error. status: %d", status)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error push release")
|
||||
}
|
||||
|
||||
c.Log.Printf("radarr release/push status: (%v) response: %v\n", status, string(res))
|
||||
|
||||
if status == http.StatusBadRequest {
|
||||
badreqResponse := make([]*BadRequestResponse, 0)
|
||||
err = json.Unmarshal(res, &badreqResponse)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
if badreqResponse[0] != nil && badreqResponse[0].PropertyName == "Title" && badreqResponse[0].ErrorMessage == "Unable to parse" {
|
||||
rejections := []string{fmt.Sprintf("unable to parse: %v", badreqResponse[0].AttemptedValue)}
|
||||
return rejections, nil
|
||||
}
|
||||
}
|
||||
|
||||
pushResponse := make([]PushResponse, 0)
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("radarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
log.Trace().Msgf("radarr release/push response status: %v body: %+v", status, string(res))
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse[0].Rejected {
|
||||
rejections := strings.Join(pushResponse[0].Rejections, ", ")
|
||||
|
||||
log.Trace().Msgf("radarr push rejected: %s - reasons: %q", release.Title, rejections)
|
||||
c.Log.Printf("radarr release/push rejected %v reasons: %q\n", release.Title, rejections)
|
||||
return pushResponse[0].Rejections, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package radarr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -90,8 +89,6 @@ func Test_client_Push(t *testing.T) {
|
|||
PublishDate: "2021-08-21T15:36:00Z",
|
||||
}},
|
||||
rejections: []string{"Could not find Some Old Movie"},
|
||||
//err: errors.New("radarr push rejected Could not find Some Old Movie"),
|
||||
//wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "push_error",
|
||||
|
@ -114,8 +111,6 @@ func Test_client_Push(t *testing.T) {
|
|||
PublishDate: "2021-08-21T15:36:00Z",
|
||||
}},
|
||||
rejections: []string{"Could not find Some Old Movie"},
|
||||
//err: errors.New("radarr push rejected Could not find Some Old Movie"),
|
||||
//wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "push_parse_error",
|
||||
|
@ -137,8 +132,8 @@ func Test_client_Push(t *testing.T) {
|
|||
Protocol: "torrent",
|
||||
PublishDate: "2021-08-21T15:36:00Z",
|
||||
}},
|
||||
err: errors.New("radarr: bad request: (status: 400 Bad Request): [\n {\n \"propertyName\": \"Title\",\n \"errorMessage\": \"Unable to parse\",\n \"attemptedValue\": \"Minx 1 epi 9 2160p\",\n \"severity\": \"error\"\n }\n]\n"),
|
||||
wantErr: true,
|
||||
rejections: []string{"unable to parse: Minx 1 epi 9 2160p"},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -177,11 +172,11 @@ func Test_client_Test(t *testing.T) {
|
|||
defer srv.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg Config
|
||||
want *SystemStatusResponse
|
||||
err error
|
||||
wantErr bool
|
||||
name string
|
||||
cfg Config
|
||||
want *SystemStatusResponse
|
||||
expectedErr string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "fetch",
|
||||
|
@ -192,9 +187,9 @@ func Test_client_Test(t *testing.T) {
|
|||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: &SystemStatusResponse{Version: "3.2.2.5080"},
|
||||
err: nil,
|
||||
wantErr: false,
|
||||
want: &SystemStatusResponse{Version: "3.2.2.5080"},
|
||||
expectedErr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "fetch_unauthorized",
|
||||
|
@ -205,9 +200,9 @@ func Test_client_Test(t *testing.T) {
|
|||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
err: errors.New("unauthorized: bad credentials"),
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
expectedErr: "unauthorized: bad credentials",
|
||||
},
|
||||
{
|
||||
name: "fetch_subfolder",
|
||||
|
@ -218,9 +213,9 @@ func Test_client_Test(t *testing.T) {
|
|||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: &SystemStatusResponse{Version: "3.2.2.5080"},
|
||||
err: nil,
|
||||
wantErr: false,
|
||||
want: &SystemStatusResponse{Version: "3.2.2.5080"},
|
||||
expectedErr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -229,7 +224,7 @@ func Test_client_Test(t *testing.T) {
|
|||
|
||||
got, err := c.Test()
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue