/
ucpError.go
48 lines (41 loc) · 1.17 KB
/
ucpError.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
package ucp
import (
"encoding/json"
log "github.com/sirupsen/logrus"
)
// erro
type errorResponse struct {
Errors []errorEntry `json:"errors"`
}
type errorEntry struct {
Code string `json:"code"`
Message string `json:"message"`
}
// ParseUCPError - This will read through the return from UCP and report the error
func ParseUCPError(response []byte) error {
e := errorResponse{}
r := errorEntry{}
// TODO
// Assuming that all main UCP calls will respond using the above JSON structure (could fail silently if that isn't the case)
err := json.Unmarshal(response, &e)
if err != nil {
return err
}
// SOMETIMES we get an array, SOMETIMES we get a single object (work out which)
log.Debugf("JSON Error Response:\n%s", response)
if len(e.Errors) == 0 {
// attempt to unmarshall a single error TODO -WTF is with the random responses
err := json.Unmarshal([]byte(response), &r)
if err != nil {
return err
}
log.Errorf("%s", r.Message)
} else {
// Appears to be numerous errors .. or an array
log.Errorf("%d reported error(s)", len(e.Errors))
for i := range e.Errors {
log.Errorf("Error: %s [%s]", e.Errors[i].Code, e.Errors[i].Message)
}
}
return nil
}