forked from DreamItGetIT/statuscake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
86 lines (68 loc) · 1.82 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package statuscake
import (
"fmt"
"strings"
)
// APIError implements the error interface an it's used when the API response has errors.
type APIError interface {
APIError() string
}
type httpError struct {
status string
statusCode int
}
func (e *httpError) Error() string {
return fmt.Sprintf("HTTP error: %d - %s", e.statusCode, e.status)
}
// ValidationError is a map where the key is the invalid field and the value is a message describing why the field is invalid.
type ValidationError map[string]string
func (e ValidationError) Error() string {
var messages []string
for k, v := range e {
m := fmt.Sprintf("%s %s", k, v)
messages = append(messages, m)
}
return strings.Join(messages, ", ")
}
type updateError struct {
Issues interface{}
Message string
}
func (e *updateError) Error() string {
var messages []string
messages = append(messages, e.Message)
if issues, ok := e.Issues.(map[string]interface{}); ok {
for k, v := range issues {
m := fmt.Sprintf("%s %s", k, v)
messages = append(messages, m)
}
} else if issues, ok := e.Issues.([]interface{}); ok {
for _, v := range issues {
m := fmt.Sprint(v)
messages = append(messages, m)
}
} else if issue, ok := e.Issues.(interface{}); ok {
m := fmt.Sprint(issue)
messages = append(messages, m)
}
return strings.Join(messages, ", ")
}
// APIError returns the error specified in the API response
func (e *updateError) APIError() string {
return e.Error()
}
type deleteError struct {
Message string
}
func (e *deleteError) Error() string {
return e.Message
}
// AuthenticationError implements the error interface and it's returned
// when API responses have authentication errors
type AuthenticationError struct {
errNo int
message string
}
func (e *AuthenticationError) Error() string {
return fmt.Sprintf("%d, %s", e.errNo, e.message)
}