Skip to content

Commit

Permalink
Better error handling and logging in Tautulli module
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Dec 12, 2023
1 parent b8e49ce commit a490580
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions modules/tautulli.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ func TautulliGetUsers(TautulliPort int, TautulliIP string, TautulliHttps bool, T

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println("Failed to read Tautulli API response body. Error: " + err.Error())
return usersReply, errors.New("Failed to read Tautulli API response body.")
}

json.Unmarshal(body, &usersReply)
err = json.Unmarshal(body, &usersReply)
if err != nil {
log.Println(err)
log.Println("Failed to parse Tautulli response. Error: " + err.Error())
return usersReply, errors.New("Failed to parse Tautulli response.")
}

Expand All @@ -163,7 +167,7 @@ func TautulliDownloadStatistics(TautulliPort int, TautulliIP string, TautulliHtt

url_string, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
if err != nil {
log.Println(err)
log.Println("Failed to build Tautulli connection URL. Error: " + err.Error())
return []models.TautulliHistoryItem{}, errors.New("Failed to build Tautulli connection URL.")
}

Expand All @@ -174,25 +178,29 @@ func TautulliDownloadStatistics(TautulliPort int, TautulliIP string, TautulliHtt

req, err := http.NewRequest("GET", url_string, payload)
if err != nil {
log.Println(err)
log.Println("Failed to reach Tautulli server. Error: " + err.Error())
return []models.TautulliHistoryItem{}, errors.New("Failed to reach Tautulli server.")
}

req.Header.Add("Accept", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
log.Println("Failed to reach Tautulli server. Error: " + err.Error())
return []models.TautulliHistoryItem{}, errors.New("Failed to reach Tautulli server.")
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println("Failed to read Tautulli API response body. Error: " + err.Error())
return []models.TautulliHistoryItem{}, errors.New("Failed to read Tautulli API response body.")
}

var body_reply models.TautulliGetHistoryReply
json.Unmarshal(body, &body_reply)
err = json.Unmarshal(body, &body_reply)
if err != nil {
log.Println(err)
log.Println("Failed to parse Tautulli response. Error: " + err.Error())
return []models.TautulliHistoryItem{}, errors.New("Failed to parse Tautulli response.")
}

Expand Down

0 comments on commit a490580

Please sign in to comment.