From 301180e55b6a0bf7ef5c47f13fda860c33a90afe Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:20:48 +0100 Subject: [PATCH] feat(download-clients): improve arr error handling (#549) --- pkg/lidarr/lidarr.go | 21 +++++++++++++-------- pkg/radarr/radarr.go | 30 +++++++++++++++++------------- pkg/radarr/radarr_test.go | 2 +- pkg/readarr/readarr.go | 25 +++++++++++++++---------- pkg/sonarr/sonarr.go | 25 +++++++++++++++---------- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/pkg/lidarr/lidarr.go b/pkg/lidarr/lidarr.go index 4e0247f..9297064 100644 --- a/pkg/lidarr/lidarr.go +++ b/pkg/lidarr/lidarr.go @@ -76,10 +76,15 @@ type PushResponse struct { type BadRequestResponse struct { PropertyName string `json:"propertyName"` ErrorMessage string `json:"errorMessage"` + ErrorCode string `json:"errorCode"` AttemptedValue string `json:"attemptedValue"` Severity string `json:"severity"` } +func (r BadRequestResponse) String() string { + return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue) +} + type SystemStatusResponse struct { Version string `json:"version"` } @@ -114,21 +119,21 @@ func (c *client) Push(release Release) ([]string, error) { 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 { + badRequestResponses := make([]*BadRequestResponse, 0) + if err = json.Unmarshal(res, &badRequestResponses); 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 + rejections := []string{} + for _, response := range badRequestResponses { + rejections = append(rejections, response.String()) } + + return rejections, nil } pushResponse := PushResponse{} - err = json.Unmarshal(res, &pushResponse) - if err != nil { + if err = json.Unmarshal(res, &pushResponse); err != nil { return nil, errors.Wrap(err, "lidarr client error json unmarshal") } diff --git a/pkg/radarr/radarr.go b/pkg/radarr/radarr.go index c3b860a..fd06f28 100644 --- a/pkg/radarr/radarr.go +++ b/pkg/radarr/radarr.go @@ -77,10 +77,15 @@ type SystemStatusResponse struct { } type BadRequestResponse struct { - PropertyName string `json:"propertyName"` - ErrorMessage string `json:"errorMessage"` - AttemptedValue string `json:"attemptedValue"` Severity string `json:"severity"` + ErrorCode string `json:"errorCode"` + ErrorMessage string `json:"errorMessage"` + PropertyName string `json:"propertyName"` + AttemptedValue string `json:"attemptedValue"` +} + +func (r *BadRequestResponse) String() string { + return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue) } func (c *client) Test() (*SystemStatusResponse, error) { @@ -94,8 +99,7 @@ func (c *client) Test() (*SystemStatusResponse, error) { } response := SystemStatusResponse{} - err = json.Unmarshal(res, &response) - if err != nil { + if err = json.Unmarshal(res, &response); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") } @@ -113,21 +117,21 @@ func (c *client) Push(release Release) ([]string, error) { 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 { + badRequestResponses := make([]*BadRequestResponse, 0) + if err = json.Unmarshal(res, &badRequestResponses); 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 + rejections := []string{} + for _, response := range badRequestResponses { + rejections = append(rejections, response.String()) } + + return rejections, nil } pushResponse := make([]PushResponse, 0) - err = json.Unmarshal(res, &pushResponse) - if err != nil { + if err = json.Unmarshal(res, &pushResponse); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") } diff --git a/pkg/radarr/radarr_test.go b/pkg/radarr/radarr_test.go index 8ff3656..346ee10 100644 --- a/pkg/radarr/radarr_test.go +++ b/pkg/radarr/radarr_test.go @@ -133,7 +133,7 @@ func Test_client_Push(t *testing.T) { Protocol: "torrent", PublishDate: "2021-08-21T15:36:00Z", }}, - rejections: []string{"unable to parse: Minx 1 epi 9 2160p"}, + rejections: []string{"[error: ] Title: Unable to parse - got value: Minx 1 epi 9 2160p"}, wantErr: false, }, } diff --git a/pkg/readarr/readarr.go b/pkg/readarr/readarr.go index 4979928..65f3736 100644 --- a/pkg/readarr/readarr.go +++ b/pkg/readarr/readarr.go @@ -78,10 +78,15 @@ type PushResponse struct { type BadRequestResponse struct { PropertyName string `json:"propertyName"` ErrorMessage string `json:"errorMessage"` + ErrorCode string `json:"errorCode"` AttemptedValue string `json:"attemptedValue"` Severity string `json:"severity"` } +func (r *BadRequestResponse) String() string { + return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue) +} + type SystemStatusResponse struct { AppName string `json:"appName"` Version string `json:"version"` @@ -100,8 +105,7 @@ func (c *client) Test() (*SystemStatusResponse, error) { c.Log.Printf("readarr system/status status: (%v) response: %v\n", status, string(res)) response := SystemStatusResponse{} - err = json.Unmarshal(res, &response) - if err != nil { + if err = json.Unmarshal(res, &response); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") } @@ -117,22 +121,23 @@ func (c *client) Push(release Release) ([]string, error) { c.Log.Printf("readarr 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 { + badRequestResponses := make([]*BadRequestResponse, 0) + + if err = json.Unmarshal(res, &badRequestResponses); 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 + rejections := []string{} + for _, response := range badRequestResponses { + rejections = append(rejections, response.String()) } + + return rejections, nil } // pushResponse := make([]PushResponse, 0) var pushResponse PushResponse - err = json.Unmarshal(res, &pushResponse) - if err != nil { + if err = json.Unmarshal(res, &pushResponse); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") } diff --git a/pkg/sonarr/sonarr.go b/pkg/sonarr/sonarr.go index 7b88698..d3bb9ef 100644 --- a/pkg/sonarr/sonarr.go +++ b/pkg/sonarr/sonarr.go @@ -78,10 +78,15 @@ type PushResponse struct { type BadRequestResponse struct { PropertyName string `json:"propertyName"` ErrorMessage string `json:"errorMessage"` + ErrorCode string `json:"errorCode"` AttemptedValue string `json:"attemptedValue"` Severity string `json:"severity"` } +func (r *BadRequestResponse) String() string { + return fmt.Sprintf("[%v: %v] %v: %v - got value: %v", r.Severity, r.ErrorCode, r.PropertyName, r.ErrorMessage, r.AttemptedValue) +} + type SystemStatusResponse struct { Version string `json:"version"` } @@ -99,8 +104,7 @@ func (c *client) Test() (*SystemStatusResponse, error) { c.Log.Printf("sonarr system/status status: (%v) response: %v\n", status, string(res)) response := SystemStatusResponse{} - err = json.Unmarshal(res, &response) - if err != nil { + if err = json.Unmarshal(res, &response); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") } @@ -116,21 +120,22 @@ func (c *client) Push(release Release) ([]string, error) { 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 { + badRequestResponses := make([]*BadRequestResponse, 0) + + if err = json.Unmarshal(res, &badRequestResponses); 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 + rejections := []string{} + for _, response := range badRequestResponses { + rejections = append(rejections, response.String()) } + + return rejections, nil } pushResponse := make([]PushResponse, 0) - err = json.Unmarshal(res, &pushResponse) - if err != nil { + if err = json.Unmarshal(res, &pushResponse); err != nil { return nil, errors.Wrap(err, "could not unmarshal data") }