-
Notifications
You must be signed in to change notification settings - Fork 17
/
errors.go
82 lines (64 loc) · 1.57 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
package virtualbox
import (
"fmt"
"strings"
)
type OperationErrorType string
type OperationError struct {
Path string
// for e.g GET, PUT, WRITE, READ
Op string
Type OperationErrorType
Err error
}
func (o OperationError) Error() string {
return strings.Join([]string{o.Path, o.Op, string(o.Type), o.Err.Error()}, ", \n")
}
type ValidationError struct {
Path string
Err error
}
func (v ValidationError) Error() string {
return v.Err.Error()
}
type ValidationErrors struct {
errors []ValidationError
}
func (v ValidationErrors) Error() string {
var messages []string
for _, v := range v.errors {
messages = append(messages, v.Error())
}
return strings.Join(messages, "\n")
}
func (v ValidationErrors) Add(path string, err error) {
v.errors = append(v.errors, ValidationError{path, err})
}
type AlreadyExists string
func (v AlreadyExists) Error() string {
return string(v)
}
func (v AlreadyExists) New(item string, hints ...string) AlreadyExists {
hint := ""
if len(hints) > 0 {
hint = strings.Join(append([]string{"Try the following: \n"}, hints...), "\n")
}
return AlreadyExists(fmt.Sprintf("%s already exists. %s", item, hint))
}
var AlreadyExistsErrorr AlreadyExists = "already exists"
func IsAlreadyExistsError(err error) bool {
_, ok := err.(AlreadyExists)
return ok
}
type AlreadyAttachedError string
func (v AlreadyAttachedError) Error() string {
return string(v)
}
func IsAlreadyAttachedError(err error) bool {
_, ok := err.(AlreadyAttachedError)
return ok
}
type NotFoundError string
func (n NotFoundError) Error() string {
return string(n)
}