fix: date time and improve erorrs (#22)

This commit is contained in:
Ludvig Lundgren 2021-08-30 22:11:59 +02:00 committed by GitHub
parent c372ca4ddf
commit d4aa2027c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 84 additions and 73 deletions

View file

@ -74,7 +74,7 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
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, nil
return nil, errors.New("sonarr: bad request")
}
// return raw response and let the caller handle json unmarshal of body

View file

@ -2,8 +2,6 @@ package sonarr
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
@ -24,7 +22,7 @@ type Config struct {
type Client interface {
Test() (*SystemStatusResponse, error)
Push(release Release) error
Push(release Release) (bool, error)
}
type client struct {
@ -71,7 +69,7 @@ type SystemStatusResponse struct {
func (c *client) Test() (*SystemStatusResponse, error) {
res, err := c.get("system/status")
if err != nil {
log.Error().Err(err).Msg("sonarr client get error")
log.Error().Stack().Err(err).Msg("sonarr client get error")
return nil, err
}
@ -79,14 +77,14 @@ func (c *client) Test() (*SystemStatusResponse, error) {
body, err := io.ReadAll(res.Body)
if err != nil {
log.Error().Err(err).Msg("sonarr client error reading body")
log.Error().Stack().Err(err).Msg("sonarr client error reading body")
return nil, err
}
response := SystemStatusResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Error().Err(err).Msg("sonarr client error json unmarshal")
log.Error().Stack().Err(err).Msg("sonarr client error json unmarshal")
return nil, err
}
@ -95,37 +93,42 @@ func (c *client) Test() (*SystemStatusResponse, error) {
return &response, nil
}
func (c *client) Push(release Release) error {
func (c *client) Push(release Release) (bool, error) {
res, err := c.post("release/push", release)
if err != nil {
log.Error().Err(err).Msg("sonarr client post error")
return err
log.Error().Stack().Err(err).Msg("sonarr client post error")
return false, err
}
if res == nil {
return false, nil
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Error().Err(err).Msg("sonarr client error reading body")
return err
log.Error().Stack().Err(err).Msg("sonarr client error reading body")
return false, err
}
pushResponse := make([]PushResponse, 0)
err = json.Unmarshal(body, &pushResponse)
if err != nil {
log.Error().Err(err).Msg("sonarr client error json unmarshal")
return err
log.Error().Stack().Err(err).Msg("sonarr client error json unmarshal")
return false, err
}
log.Trace().Msgf("sonarr release/push response body: %+v", string(body))
log.Trace().Msgf("sonarr release/push response body: %+v", body)
// 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)
return errors.New(fmt.Errorf("sonarr push rejected %v", rejections).Error())
return false, nil
}
return nil
// successful push
return true, nil
}

View file

@ -104,7 +104,7 @@ func Test_client_Push(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
c := New(tt.fields.config)
err := c.Push(tt.args.release)
_, err := c.Push(tt.args.release)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.err, err)
}