-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
56 lines (48 loc) · 1.04 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package radarr
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func parseRadarrResponse(response *http.Response) error {
// If ok, send no error
if response.StatusCode == http.StatusOK {
return nil
}
e := Error{}
e.Code = response.StatusCode
e.Message = "Unknown"
// Read response body as plain text
data, _ := ioutil.ReadAll(response.Body)
// Because Radarr response contains 'error' key or 'message' key. Parse it, and set to e.Message
var body map[string]string
err := json.Unmarshal(data, &body)
if err != nil {
switch {
case len(data) == 0:
// No body
return &e
default:
// Body as plain text
e.Message = string(data)
return &e
}
}
// If JSON decoding do not fail
switch {
case body["error"] != "":
e.Message = body["error"]
return &e
case body["message"] != "":
e.Message = body["message"]
return &e
default:
var m string
for key, value := range body {
m += fmt.Sprintf("%s=%s", key, value)
}
e.Message = fmt.Sprintf("Unable to read Radarr response body: %s", m)
return &e
}
}