mirror of
https://github.com/idanoo/autobrr
synced 2025-07-24 01:09:13 +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
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue