Better error from Tautulli connection test

This commit is contained in:
aunefyren 2022-12-11 17:17:06 +01:00
parent f3b8923e32
commit 8b5ace3fa7
2 changed files with 35 additions and 13 deletions

View file

@ -15,39 +15,56 @@ import (
func TautulliTestConnection(TautulliPort int, TautulliIP string, TautulliHttps bool, TautulliRoot string, TautulliApiKey string) (bool, error) {
url_string, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
urlString, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
if err != nil {
log.Println(err)
errString := strings.Replace(err.Error(), TautulliApiKey, "REDACTED", -1)
log.Println("Failed to build Tautulli connection URL. Error: " + errString)
return false, errors.New("Failed to build Tautulli connection URL.")
}
url_string = url_string + "api/v2/" + "?apikey=" + TautulliApiKey + "&cmd=status"
urlString = urlString + "api/v2/" + "?apikey=" + TautulliApiKey + "&cmd=status"
params := url.Values{}
payload := strings.NewReader(params.Encode())
req, err := http.NewRequest("GET", url_string, payload)
req, err := http.NewRequest("GET", urlString, payload)
if err != nil {
log.Println(err)
return false, errors.New("Failed to reach Tautulli server.")
errString := strings.Replace(err.Error(), TautulliApiKey, "REDACTED", -1)
log.Println("Failed to reach Tautulli server. Error: " + errString)
return false, errors.New("Failed to reach Tautulli server. Error: " + errString)
}
req.Header.Add("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return false, errors.New("Failed to reach Tautulli server.")
errString := strings.Replace(err.Error(), TautulliApiKey, "REDACTED", -1)
log.Println("Failed to reach Tautulli server. Error: " + errString)
return false, errors.New("Failed to reach Tautulli server. Error: " + errString)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
errString := strings.Replace(err.Error(), TautulliApiKey, "REDACTED", -1)
log.Println("Failed to read Tautulli server response. Error: " + errString)
return false, errors.New("Failed to read Tautulli response. Error: " + errString)
} else if res.StatusCode != 200 {
errString := "Tautulli didn't respond with status code 200, got: " + res.Status + " instead."
reply := string(body)
replyString := strings.Replace(reply, TautulliApiKey, "REDACTED", -1)
log.Println("Failed to connect to Tautulli server. \n\nReply: " + replyString + ". +n\nError: " + errString)
return false, errors.New("Failed to connect to Tautulli server. \n\nReply: " + replyString + ". \n\nError: " + errString)
}
var body_reply models.TautulliStatusReply
json.Unmarshal(body, &body_reply)
err = json.Unmarshal(body, &body_reply)
if err != nil {
log.Println(err)
return false, errors.New("Failed to parse Tautulli response.")
errString := strings.Replace(err.Error(), TautulliApiKey, "REDACTED", -1)
reply := string(body)
replyString := strings.Replace(reply, TautulliApiKey, "REDACTED", -1)
log.Println("Failed to parse Tautulli server response. \n\nReply: " + replyString + ". +n\nError: " + errString)
return false, errors.New("Failed to parse Tautulli server response. \n\nReply: " + replyString + ". \n\nError: " + errString)
}
var tautulli_status bool = false
@ -56,6 +73,12 @@ func TautulliTestConnection(TautulliPort int, TautulliIP string, TautulliHttps b
tautulli_status = true
} else if body_reply.Response.Result == "error" {
errString := strings.Replace(body_reply.Response.Message, TautulliApiKey, "REDACTED", -1)
log.Println("Tautulli server responsed with an error. Error: " + errString)
return false, errors.New("Tautulli server responsed with an error. Error: " + errString)
}
return tautulli_status, nil

View file

@ -304,8 +304,7 @@ func ApiGetTautulliConncection(w http.ResponseWriter, r *http.Request) {
tautulli_state, err := modules.TautulliTestConnection(tautulli_connection.TautulliPort, tautulli_connection.TautulliIP, tautulli_connection.TautulliHttps, tautulli_connection.TautulliRoot, tautulli_connection.TautulliApiKey)
if err != nil {
log.Println(err)
utilities.RespondDefaultError(w, r, errors.New("Failed to reach Tautulli server."), 500)
utilities.RespondDefaultError(w, r, err, 500)
return
}