-
Notifications
You must be signed in to change notification settings - Fork 90
/
errors.go
56 lines (46 loc) · 1.17 KB
/
errors.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 registry
import (
"encoding/json"
"fmt"
"strings"
)
type registryError struct {
Code string `json:"code"`
Message string `json:"message"`
Detail string `json:"detail"`
}
type dockerIOError struct {
Details string `json:"details,omitempty"`
}
type registryErrors struct {
Errors []registryError `json:"errors"`
}
func errorResponseToString(statusCode int, response []byte) string {
if len(response) == 0 {
return fmt.Sprintf("unexpected status code %d", statusCode)
}
e := ®istryErrors{}
_ = json.Unmarshal(response, e)
messages := make([]string, 0)
for _, err := range e.Errors {
if err.Message != "" && err.Detail != "" {
messages = append(messages, fmt.Sprintf("%s: %s", err.Message, err.Detail))
} else if err.Message != "" {
messages = append(messages, err.Message)
} else if err.Detail != "" {
messages = append(messages, err.Detail)
} else {
messages = append(messages, err.Code)
}
}
ee := &dockerIOError{}
_ = json.Unmarshal(response, ee)
if ee.Details != "" {
messages = append(messages, ee.Details)
}
errResponse := strings.Join(messages, "\n")
if errResponse == "" {
return string(response)
}
return errResponse
}