]>
Commit | Line | Data |
---|---|---|
9b12e4fe JC |
1 | package statuscake |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "strings" | |
6 | ) | |
7 | ||
8 | // APIError implements the error interface an it's used when the API response has errors. | |
9 | type APIError interface { | |
10 | APIError() string | |
11 | } | |
12 | ||
13 | type httpError struct { | |
14 | status string | |
15 | statusCode int | |
16 | } | |
17 | ||
18 | func (e *httpError) Error() string { | |
19 | return fmt.Sprintf("HTTP error: %d - %s", e.statusCode, e.status) | |
20 | } | |
21 | ||
22 | // ValidationError is a map where the key is the invalid field and the value is a message describing why the field is invalid. | |
23 | type ValidationError map[string]string | |
24 | ||
25 | func (e ValidationError) Error() string { | |
26 | var messages []string | |
27 | ||
28 | for k, v := range e { | |
29 | m := fmt.Sprintf("%s %s", k, v) | |
30 | messages = append(messages, m) | |
31 | } | |
32 | ||
33 | return strings.Join(messages, ", ") | |
34 | } | |
35 | ||
36 | type updateError struct { | |
ccc9fb69 MS |
37 | Issues interface{} |
38 | Message string | |
9b12e4fe JC |
39 | } |
40 | ||
41 | func (e *updateError) Error() string { | |
42 | var messages []string | |
43 | ||
ccc9fb69 MS |
44 | messages = append(messages, e.Message) |
45 | ||
9b12e4fe JC |
46 | if issues, ok := e.Issues.(map[string]interface{}); ok { |
47 | for k, v := range issues { | |
48 | m := fmt.Sprintf("%s %s", k, v) | |
49 | messages = append(messages, m) | |
50 | } | |
51 | } else if issues, ok := e.Issues.([]interface{}); ok { | |
52 | for _, v := range issues { | |
53 | m := fmt.Sprint(v) | |
54 | messages = append(messages, m) | |
55 | } | |
ccc9fb69 MS |
56 | } else if issue, ok := e.Issues.(interface{}); ok { |
57 | m := fmt.Sprint(issue) | |
58 | messages = append(messages, m) | |
9b12e4fe JC |
59 | } |
60 | ||
61 | return strings.Join(messages, ", ") | |
62 | } | |
63 | ||
64 | // APIError returns the error specified in the API response | |
65 | func (e *updateError) APIError() string { | |
66 | return e.Error() | |
67 | } | |
68 | ||
69 | type deleteError struct { | |
70 | Message string | |
71 | } | |
72 | ||
73 | func (e *deleteError) Error() string { | |
74 | return e.Message | |
75 | } | |
76 | ||
77 | // AuthenticationError implements the error interface and it's returned | |
78 | // when API responses have authentication errors | |
79 | type AuthenticationError struct { | |
80 | errNo int | |
81 | message string | |
82 | } | |
83 | ||
84 | func (e *AuthenticationError) Error() string { | |
85 | return fmt.Sprintf("%d, %s", e.errNo, e.message) | |
86 | } |