util.go 4.3 KB

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