blob: 799c1197d8dd356b0f7268bab91b16a98a60802e [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package v3rpc
16
17import (
18 "context"
19 "strings"
20
21 "github.com/coreos/etcd/auth"
22 "github.com/coreos/etcd/etcdserver"
23 "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
24 "github.com/coreos/etcd/etcdserver/membership"
25 "github.com/coreos/etcd/lease"
26 "github.com/coreos/etcd/mvcc"
27
28 "google.golang.org/grpc/codes"
29 "google.golang.org/grpc/status"
30)
31
32var toGRPCErrorMap = map[error]error{
33 membership.ErrIDRemoved: rpctypes.ErrGRPCMemberNotFound,
34 membership.ErrIDNotFound: rpctypes.ErrGRPCMemberNotFound,
35 membership.ErrIDExists: rpctypes.ErrGRPCMemberExist,
36 membership.ErrPeerURLexists: rpctypes.ErrGRPCPeerURLExist,
37 etcdserver.ErrNotEnoughStartedMembers: rpctypes.ErrMemberNotEnoughStarted,
38
39 mvcc.ErrCompacted: rpctypes.ErrGRPCCompacted,
40 mvcc.ErrFutureRev: rpctypes.ErrGRPCFutureRev,
41 etcdserver.ErrRequestTooLarge: rpctypes.ErrGRPCRequestTooLarge,
42 etcdserver.ErrNoSpace: rpctypes.ErrGRPCNoSpace,
43 etcdserver.ErrTooManyRequests: rpctypes.ErrTooManyRequests,
44
45 etcdserver.ErrNoLeader: rpctypes.ErrGRPCNoLeader,
46 etcdserver.ErrNotLeader: rpctypes.ErrGRPCNotLeader,
47 etcdserver.ErrStopped: rpctypes.ErrGRPCStopped,
48 etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
49 etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
50 etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
51 etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
52 etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
53 etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
54
55 lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
56 lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
57 lease.ErrLeaseTTLTooLarge: rpctypes.ErrGRPCLeaseTTLTooLarge,
58
59 auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
60 auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
61 auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
62 auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
63 auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
64 auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
65 auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
66 auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
67 auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
68 auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
69 auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
70 auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
71 auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
72 auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
73}
74
75func togRPCError(err error) error {
76 // let gRPC server convert to codes.Canceled, codes.DeadlineExceeded
77 if err == context.Canceled || err == context.DeadlineExceeded {
78 return err
79 }
80 grpcErr, ok := toGRPCErrorMap[err]
81 if !ok {
82 return status.Error(codes.Unknown, err.Error())
83 }
84 return grpcErr
85}
86
87func isClientCtxErr(ctxErr error, err error) bool {
88 if ctxErr != nil {
89 return true
90 }
91
92 ev, ok := status.FromError(err)
93 if !ok {
94 return false
95 }
96
97 switch ev.Code() {
98 case codes.Canceled, codes.DeadlineExceeded:
99 // client-side context cancel or deadline exceeded
100 // "rpc error: code = Canceled desc = context canceled"
101 // "rpc error: code = DeadlineExceeded desc = context deadline exceeded"
102 return true
103 case codes.Unavailable:
104 msg := ev.Message()
105 // client-side context cancel or deadline exceeded with TLS ("http2.errClientDisconnected")
106 // "rpc error: code = Unavailable desc = client disconnected"
107 if msg == "client disconnected" {
108 return true
109 }
110 // "grpc/transport.ClientTransport.CloseStream" on canceled streams
111 // "rpc error: code = Unavailable desc = stream error: stream ID 21; CANCEL")
112 if strings.HasPrefix(msg, "stream error: ") && strings.HasSuffix(msg, "; CANCEL") {
113 return true
114 }
115 }
116 return false
117}