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,12 +3,12 @@ package lidarr
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
|
@ -18,7 +18,7 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return 0, nil, errors.New(fmt.Sprintf("lidarr client request error : %v", reqUrl))
|
||||
return 0, nil, errors.Wrap(err, "lidarr client request error : %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -29,14 +29,14 @@ func (c *client) get(endpoint string) (int, []byte, error) {
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("lidarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("lidarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy error")
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
|
@ -49,12 +49,12 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client could not marshal data: %v", reqUrl)
|
||||
return nil, errors.Wrap(err, "lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
return nil, errors.Wrap(err, "lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -67,7 +67,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
return nil, errors.Wrap(err, "lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
// validate response
|
||||
|
@ -88,12 +88,12 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("lidarr client could not marshal data: %v", reqUrl)
|
||||
return 0, nil, errors.Wrap(err, "lidarr client could not marshal data: %v", reqUrl)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("lidarr client request error: %v", reqUrl)
|
||||
return 0, nil, errors.Wrap(err, "lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
|
@ -104,18 +104,20 @@ func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("lidarr.http.Do(req): %w", err)
|
||||
return 0, nil, errors.Wrap(err, "lidarr.http.Do(req)")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("lidarr.io.Copy: %w", err)
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "lidarr.io.Copy")
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return resp.StatusCode, buf.Bytes(), fmt.Errorf("lidarr: 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("lidarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
|
|
|
@ -2,11 +2,14 @@ package lidarr
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -17,6 +20,8 @@ type Config struct {
|
|||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
|
@ -27,6 +32,8 @@ type Client interface {
|
|||
type client struct {
|
||||
config Config
|
||||
http *http.Client
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
// New create new lidarr client
|
||||
|
@ -39,6 +46,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
|
||||
|
@ -61,6 +73,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"`
|
||||
}
|
||||
|
@ -68,43 +87,56 @@ type SystemStatusResponse struct {
|
|||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client get error: %w", err)
|
||||
return nil, errors.Wrap(err, "lidarr client get error")
|
||||
}
|
||||
|
||||
if status == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
}
|
||||
|
||||
//log.Trace().Msgf("lidarr system/status response status: %v body: %v", status, string(res))
|
||||
c.Log.Printf("lidarr system/status response status: %v body: %v", status, string(res))
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client error json unmarshal: %w", err)
|
||||
return nil, errors.Wrap(err, "lidarr client error json unmarshal")
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
_, res, err := c.postBody("release/push", release)
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client post error: %w", err)
|
||||
return nil, errors.Wrap(err, "lidarr client post error")
|
||||
}
|
||||
|
||||
//log.Trace().Msgf("lidarr release/push response status: %v body: %v", status, string(res))
|
||||
c.Log.Printf("lidarr release/push response status: %v body: %v", 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 := PushResponse{}
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lidarr client error json unmarshal: %w", err)
|
||||
return nil, errors.Wrap(err, "lidarr client error json unmarshal")
|
||||
}
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse.Rejected {
|
||||
rejections := strings.Join(pushResponse.Rejections, ", ")
|
||||
|
||||
return pushResponse.Rejections, fmt.Errorf("lidarr push rejected: %s - reasons: %q: err %w", release.Title, rejections, err)
|
||||
return pushResponse.Rejections, errors.New("lidarr push rejected: %s - reasons: %q", release.Title, rejections)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package lidarr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -134,11 +133,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",
|
||||
|
@ -149,9 +148,9 @@ func Test_client_Test(t *testing.T) {
|
|||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: &SystemStatusResponse{Version: "0.8.1.2135"},
|
||||
err: nil,
|
||||
wantErr: false,
|
||||
want: &SystemStatusResponse{Version: "0.8.1.2135"},
|
||||
expectedErr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "fetch_unauthorized",
|
||||
|
@ -162,9 +161,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 {
|
||||
|
@ -173,7 +172,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