-
Notifications
You must be signed in to change notification settings - Fork 567
/
errors.go
91 lines (74 loc) · 2.12 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
87
88
89
90
91
package collection
import (
"fmt"
"strings"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ErrNotFound indicates that a key was not found when it was expected to
// exist.
type ErrNotFound struct {
Type string
Key string
customMessage string
}
func (err ErrNotFound) Is(other error) bool {
_, ok := other.(ErrNotFound)
return ok
}
func (err ErrNotFound) Error() string {
if err.customMessage != "" {
return err.customMessage
}
return fmt.Sprintf("%s %s not found", strings.TrimPrefix(err.Type, DefaultPrefix), err.Key)
}
func (e ErrNotFound) GRPCStatus() *status.Status {
return status.New(codes.NotFound, e.Error())
}
// IsErrNotFound determines if an error is an ErrNotFound error
func IsErrNotFound(err error) bool {
return errors.Is(err, ErrNotFound{})
}
// ErrExists indicates that a key was found to exist when it was expected not
// to.
type ErrExists struct {
Type string
Key string
customMessage string
}
func (err ErrExists) Is(other error) bool {
_, ok := other.(ErrExists)
return ok
}
func (err ErrExists) Error() string {
if err.customMessage != "" {
return err.customMessage
}
return fmt.Sprintf("%s %s already exists", strings.TrimPrefix(err.Type, DefaultPrefix), err.Key)
}
func (e ErrExists) GRPCStatus() *status.Status {
return status.New(codes.AlreadyExists, e.Error())
}
// IsErrExists determines if an error is an ErrExists error
func IsErrExists(err error) bool {
return errors.Is(err, ErrExists{})
}
// ErrNotUnique indicates that an indexed query expected to have exactly one
// result but had more than one result.
type ErrNotUnique struct {
Type string
Index string
Value string
}
func (err ErrNotUnique) Is(other error) bool {
_, ok := other.(ErrNotUnique)
return ok
}
func (err ErrNotUnique) Error() string {
return fmt.Sprintf("more than one matching row found in %s on index %s with value %s", err.Type, err.Index, err.Value)
}
// IsErrNotUnique determines if an error is an ErrNotUnique error
func IsErrNotUnique(err error) bool {
return errors.Is(err, ErrNotUnique{})
}