]>
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 { | |
37 | Issues interface{} | |
38 | } | |
39 | ||
40 | func (e *updateError) Error() string { | |
41 | var messages []string | |
42 | ||
43 | if issues, ok := e.Issues.(map[string]interface{}); ok { | |
44 | for k, v := range issues { | |
45 | m := fmt.Sprintf("%s %s", k, v) | |
46 | messages = append(messages, m) | |
47 | } | |
48 | } else if issues, ok := e.Issues.([]interface{}); ok { | |
49 | for _, v := range issues { | |
50 | m := fmt.Sprint(v) | |
51 | messages = append(messages, m) | |
52 | } | |
53 | } | |
54 | ||
55 | return strings.Join(messages, ", ") | |
56 | } | |
57 | ||
58 | // APIError returns the error specified in the API response | |
59 | func (e *updateError) APIError() string { | |
60 | return e.Error() | |
61 | } | |
62 | ||
63 | type deleteError struct { | |
64 | Message string | |
65 | } | |
66 | ||
67 | func (e *deleteError) Error() string { | |
68 | return e.Message | |
69 | } | |
70 | ||
71 | // AuthenticationError implements the error interface and it's returned | |
72 | // when API responses have authentication errors | |
73 | type AuthenticationError struct { | |
74 | errNo int | |
75 | message string | |
76 | } | |
77 | ||
78 | func (e *AuthenticationError) Error() string { | |
79 | return fmt.Sprintf("%d, %s", e.errNo, e.message) | |
80 | } |