util.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // 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. package v3rpc
  15. import (
  16. "context"
  17. "strings"
  18. "github.com/coreos/etcd/auth"
  19. "github.com/coreos/etcd/etcdserver"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  21. "github.com/coreos/etcd/etcdserver/membership"
  22. "github.com/coreos/etcd/lease"
  23. "github.com/coreos/etcd/mvcc"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/status"
  26. )
  27. var toGRPCErrorMap = map[error]error{
  28. membership.ErrIDRemoved: rpctypes.ErrGRPCMemberNotFound,
  29. membership.ErrIDNotFound: rpctypes.ErrGRPCMemberNotFound,
  30. membership.ErrIDExists: rpctypes.ErrGRPCMemberExist,
  31. membership.ErrPeerURLexists: rpctypes.ErrGRPCPeerURLExist,
  32. etcdserver.ErrNotEnoughStartedMembers: rpctypes.ErrMemberNotEnoughStarted,
  33. mvcc.ErrCompacted: rpctypes.ErrGRPCCompacted,
  34. mvcc.ErrFutureRev: rpctypes.ErrGRPCFutureRev,
  35. etcdserver.ErrRequestTooLarge: rpctypes.ErrGRPCRequestTooLarge,
  36. etcdserver.ErrNoSpace: rpctypes.ErrGRPCNoSpace,
  37. etcdserver.ErrTooManyRequests: rpctypes.ErrTooManyRequests,
  38. etcdserver.ErrNoLeader: rpctypes.ErrGRPCNoLeader,
  39. etcdserver.ErrNotLeader: rpctypes.ErrGRPCNotLeader,
  40. etcdserver.ErrLeaderChanged: rpctypes.ErrGRPCLeaderChanged,
  41. etcdserver.ErrStopped: rpctypes.ErrGRPCStopped,
  42. etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
  43. etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
  44. etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
  45. etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
  46. etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
  47. etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
  48. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  49. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  50. lease.ErrLeaseTTLTooLarge: rpctypes.ErrGRPCLeaseTTLTooLarge,
  51. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  52. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  53. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  54. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  55. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  56. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  57. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  58. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  59. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  60. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  61. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  62. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  63. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  64. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  65. }
  66. func togRPCError(err error) error {
  67. // let gRPC server convert to codes.Canceled, codes.DeadlineExceeded
  68. if err == context.Canceled || err == context.DeadlineExceeded {
  69. return err
  70. }
  71. grpcErr, ok := toGRPCErrorMap[err]
  72. if !ok {
  73. return status.Error(codes.Unknown, err.Error())
  74. }
  75. return grpcErr
  76. }
  77. func isClientCtxErr(ctxErr error, err error) bool {
  78. if ctxErr != nil {
  79. return true
  80. }
  81. ev, ok := status.FromError(err)
  82. if !ok {
  83. return false
  84. }
  85. switch ev.Code() {
  86. case codes.Canceled, codes.DeadlineExceeded:
  87. // client-side context cancel or deadline exceeded
  88. // "rpc error: code = Canceled desc = context canceled"
  89. // "rpc error: code = DeadlineExceeded desc = context deadline exceeded"
  90. return true
  91. case codes.Unavailable:
  92. msg := ev.Message()
  93. // client-side context cancel or deadline exceeded with TLS ("http2.errClientDisconnected")
  94. // "rpc error: code = Unavailable desc = client disconnected"
  95. if msg == "client disconnected" {
  96. return true
  97. }
  98. // "grpc/transport.ClientTransport.CloseStream" on canceled streams
  99. // "rpc error: code = Unavailable desc = stream error: stream ID 21; CANCEL")
  100. if strings.HasPrefix(msg, "stream error: ") && strings.HasSuffix(msg, "; CANCEL") {
  101. return true
  102. }
  103. }
  104. return false
  105. }