/
errors.go
58 lines (48 loc) · 1.35 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
package collection
import (
"fmt"
"strings"
)
// ErrNotFound indicates that a key was not found when it was expected to
// exist.
type ErrNotFound struct {
Type string
Key string
}
func (e ErrNotFound) Error() string {
return fmt.Sprintf("%s %s not found", strings.TrimPrefix(e.Type, DefaultPrefix), e.Key)
}
// IsErrNotFound determines if an error is an ErrNotFound error
func IsErrNotFound(e error) bool {
_, ok := e.(ErrNotFound)
return ok
}
// ErrExists indicates that a key was found to exist when it was expected not
// to.
type ErrExists struct {
Type string
Key string
}
func (e ErrExists) Error() string {
return fmt.Sprintf("%s %s already exists", strings.TrimPrefix(e.Type, DefaultPrefix), e.Key)
}
// IsErrExists determines if an error is an ErrExists error
func IsErrExists(e error) bool {
_, ok := e.(ErrExists)
return ok
}
// ErrMalformedValue indicates that a value was malformed, such as when it was
// supposed to be parseable as an int but wasn't.
type ErrMalformedValue struct {
Type string
Key string
Val string
}
func (e ErrMalformedValue) Error() string {
return fmt.Sprintf("malformed value at %s/%s: %s", strings.TrimPrefix(e.Type, DefaultPrefix), e.Key, e.Val)
}
// IsErrMalformedValue determines if an error is an ErrMalformedValue error
func IsErrMalformedValue(e error) bool {
_, ok := e.(ErrMalformedValue)
return ok
}