-
Notifications
You must be signed in to change notification settings - Fork 567
/
common.go
68 lines (54 loc) · 1.38 KB
/
common.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
package pacherr
import (
"fmt"
"os"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
)
type ErrNotExist struct {
Collection string
ID string
}
func NewNotExist(collection, id string) error {
return ErrNotExist{
Collection: collection,
ID: id,
}
}
func (e ErrNotExist) Error() string {
return fmt.Sprintf("%s does not contain item: (%s)", e.Collection, e.ID)
}
func (e ErrNotExist) GRPCStatus() *status.Status {
return status.New(codes.NotFound, e.Error())
}
func IsNotExist(err error) bool {
target := ErrNotExist{}
return errors.As(err, &target) || os.IsNotExist(err) || os.IsNotExist(errors.Unwrap(err))
}
type ErrExists struct {
Collection string
ID string
}
func NewExists(collection, id string) error {
return &ErrExists{
Collection: collection,
ID: id,
}
}
func (e ErrExists) Error() string {
return fmt.Sprintf("%s already contains an item: (%s)", e.Collection, e.ID)
}
func (e ErrExists) GRPCStatus() *status.Status {
return status.New(codes.AlreadyExists, e.Error())
}
func IsExists(err error) bool {
target := ErrExists{}
return errors.As(err, &target)
}
var (
// ErrBreak is an error used to break out of call back based iteration,
// should be swallowed by iteration functions and treated as successful
// iteration.
ErrBreak = errors.Errorf("BREAK")
)