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 sonarr
|
|||
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("sonarr client request error : %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
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("sonarr client.get request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("sonarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "sonarr.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("sonarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "sonarr.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("sonarr 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("sonarr client request error: %v", reqUrl)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -73,16 +67,13 @@ 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("sonarr 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("sonarr client bad request: %v", reqUrl)
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
log.Error().Err(err).Msgf("sonarr client request error: %v", reqUrl)
|
||||
return nil, errors.New("sonarr: bad request")
|
||||
}
|
||||
|
||||
|
@ -97,14 +88,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("sonarr 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("sonarr client request error: %v", reqUrl)
|
||||
return 0, nil, err
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -115,19 +104,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("sonarr client request error: %v", reqUrl)
|
||||
return 0, nil, fmt.Errorf("sonarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "sonarr.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("sonarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "sonarr.io.Copy")
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return resp.StatusCode, buf.Bytes(), fmt.Errorf("sonarr: 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("sonarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
|
|
|
@ -2,12 +2,15 @@ package sonarr
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"log"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -18,6 +21,8 @@ type Config struct {
|
|||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
|
@ -28,6 +33,8 @@ type Client interface {
|
|||
type client struct {
|
||||
config Config
|
||||
http *http.Client
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
// New create new sonarr client
|
||||
|
@ -40,6 +47,12 @@ func New(config Config) Client {
|
|||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
// if no provided logger then use io.Discard
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
|
||||
return c
|
||||
|
@ -62,6 +75,13 @@ type PushResponse struct {
|
|||
Rejections []string `json:"rejections"`
|
||||
}
|
||||
|
||||
type BadRequestResponse struct {
|
||||
PropertyName string `json:"propertyName"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
AttemptedValue string `json:"attemptedValue"`
|
||||
Severity string `json:"severity"`
|
||||
}
|
||||
|
||||
type SystemStatusResponse struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
@ -69,21 +89,19 @@ type SystemStatusResponse struct {
|
|||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("sonarr client get error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not make Test")
|
||||
}
|
||||
|
||||
if status == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
}
|
||||
|
||||
log.Trace().Msgf("sonarr system/status response: %v", string(res))
|
||||
c.Log.Printf("sonarr system/status status: (%v) response: %v\n", status, string(res))
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("sonarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
|
@ -92,24 +110,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).Msg("sonarr client post error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not push release to sonarr")
|
||||
}
|
||||
|
||||
log.Trace().Msgf("sonarr release/push response status: (%v) body: %v", status, string(res))
|
||||
c.Log.Printf("sonarr 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, err
|
||||
}
|
||||
}
|
||||
|
||||
pushResponse := make([]PushResponse, 0)
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("sonarr client error json unmarshal")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse[0].Rejected {
|
||||
rejections := strings.Join(pushResponse[0].Rejections, ", ")
|
||||
|
||||
log.Trace().Msgf("sonarr push rejected: %s - reasons: %q", release.Title, rejections)
|
||||
c.Log.Printf("sonarr release/push rejected %v reasons: %q\n", release.Title, rejections)
|
||||
return pushResponse[0].Rejections, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package sonarr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -15,6 +15,7 @@ import (
|
|||
func Test_client_Push(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
ts := httptest.NewServer(mux)
|
||||
|
@ -119,6 +120,7 @@ func Test_client_Push(t *testing.T) {
|
|||
func Test_client_Test(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
|
@ -139,11 +141,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",
|
||||
|
@ -154,9 +156,9 @@ func Test_client_Test(t *testing.T) {
|
|||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: &SystemStatusResponse{Version: "3.0.6.1196"},
|
||||
err: nil,
|
||||
wantErr: false,
|
||||
want: &SystemStatusResponse{Version: "3.0.6.1196"},
|
||||
expectedErr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "fetch_unauthorized",
|
||||
|
@ -167,9 +169,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",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -178,7 +180,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