-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
94 lines (82 loc) · 3.25 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
92
93
94
package dbadger
import (
"errors"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/w1ck3dg0ph3r/dbadger/internal/rpc"
)
// Errors that can be returned from [DB] methods.
var (
// ErrNoLeader is returned if a cluster has no leader or the leader is unknown at the moment.
ErrNoLeader = errors.New("cluster has no leader")
// ErrEmptyKey is returned if an empty key is passed to a write operation.
ErrEmptyKey = errors.New("key can not be empty")
// ErrInvalidKey is returned if the key has a special !badger! prefix, reserved for internal usage.
ErrInvalidKey = errors.New("key is using a reserved !badger! prefix")
// ErrNotFound is returned when key is not found in read operations.
ErrNotFound = errors.New("key not found")
// ErrInvalidRequest is returned if the user request is invalid.
ErrInvalidRequest = errors.New("invalid request")
// ErrConflict is returned when a transaction conflicts with another transaction. This can
// happen if the read rows had been updated concurrently by another transaction.
ErrConflict = errors.New("transaction conflict, please retry")
// ErrUnavailable is returned when the cluster is currently unable to perform the
// requested operation, but the operation should be retried with a backoff.
// This can mean network errors, blocked writes during DeleteAll, etc.
ErrUnavailable = errors.New("operation is currently unavailable, please retry")
ErrInternal = errors.New("internal error")
)
// GRPC status errors.
//
//nolint:errname,gochecknoglobals
var (
statusEmptyKey = newGRPCError(codes.InvalidArgument, ErrEmptyKey.Error(), rpc.Error_EMPTY_KEY)
statusInvalidKey = newGRPCError(codes.InvalidArgument, ErrInvalidKey.Error(), rpc.Error_INVALID_KEY)
statusNotFound = newGRPCError(codes.NotFound, ErrNotFound.Error(), rpc.Error_NOT_FOUND)
statusInvalidRequest = newGRPCError(codes.InvalidArgument, ErrInvalidRequest.Error(), rpc.Error_INVALID_REQUEST)
statusConflict = newGRPCError(codes.Unavailable, ErrConflict.Error(), rpc.Error_CONFLICT)
statusUnavailable = newGRPCError(codes.Unavailable, ErrUnavailable.Error(), rpc.Error_UNAVAILABLE)
)
func newGRPCError(rpcCode codes.Code, msg string, code rpc.Error_Code) error {
st, err := status.New(rpcCode, msg).WithDetails(&rpc.Error{Code: code})
if err != nil {
panic(err)
}
return st.Err()
}
func mapError(err error) error {
if err == nil {
return nil
}
if st, ok := status.FromError(err); ok {
if len(st.Details()) > 0 {
if e, ok := st.Details()[0].(*rpc.Error); ok {
switch e.Code {
case rpc.Error_NO_LEADER:
return ErrNoLeader
case rpc.Error_EMPTY_KEY:
return ErrEmptyKey
case rpc.Error_INVALID_KEY:
return ErrInvalidKey
case rpc.Error_NOT_FOUND:
return ErrNotFound
case rpc.Error_INVALID_REQUEST:
return ErrInvalidRequest
case rpc.Error_CONFLICT:
return ErrConflict
case rpc.Error_UNAVAILABLE:
return ErrUnavailable
}
}
}
switch st.Code() {
case codes.Unavailable,
codes.Canceled:
return fmt.Errorf("%w: %v", ErrUnavailable, err) //nolint:errorlint // We want to wrap the first error only.
default:
return fmt.Errorf("%w: %v", ErrInternal, err) //nolint:errorlint // We want to wrap the first error only.
}
}
return err
}