util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.ErrStopped: rpctypes.ErrGRPCStopped,
  41. etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
  42. etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
  43. etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
  44. etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
  45. etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
  46. etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
  47. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  48. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  49. lease.ErrLeaseTTLTooLarge: rpctypes.ErrGRPCLeaseTTLTooLarge,
  50. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  51. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  52. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  53. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  54. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  55. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  56. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  57. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  58. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  59. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  60. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  61. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  62. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  63. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  64. }
  65. func togRPCError(err error) error {
  66. // let gRPC server convert to codes.Canceled, codes.DeadlineExceeded
  67. if err == context.Canceled || err == context.DeadlineExceeded {
  68. return err
  69. }
  70. grpcErr, ok := toGRPCErrorMap[err]
  71. if !ok {
  72. return status.Error(codes.Unknown, err.Error())
  73. }
  74. return grpcErr
  75. }
  76. func isClientCtxErr(ctxErr error, err error) bool {
  77. if ctxErr != nil {
  78. return true
  79. }
  80. ev, ok := status.FromError(err)
  81. if !ok {
  82. return false
  83. }
  84. switch ev.Code() {
  85. case codes.Canceled, codes.DeadlineExceeded:
  86. // client-side context cancel or deadline exceeded
  87. // "rpc error: code = Canceled desc = context canceled"
  88. // "rpc error: code = DeadlineExceeded desc = context deadline exceeded"
  89. return true
  90. case codes.Unavailable:
  91. msg := ev.Message()
  92. // client-side context cancel or deadline exceeded with TLS ("http2.errClientDisconnected")
  93. // "rpc error: code = Unavailable desc = client disconnected"
  94. if msg == "client disconnected" {
  95. return true
  96. }
  97. // "grpc/transport.ClientTransport.CloseStream" on canceled streams
  98. // "rpc error: code = Unavailable desc = stream error: stream ID 21; CANCEL")
  99. if strings.HasPrefix(msg, "stream error: ") && strings.HasSuffix(msg, "; CANCEL") {
  100. return true
  101. }
  102. }
  103. return false
  104. }