util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "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/codes"
  24. "google.golang.org/grpc/status"
  25. )
  26. var toGRPCErrorMap = map[error]error{
  27. membership.ErrIDRemoved: rpctypes.ErrGRPCMemberNotFound,
  28. membership.ErrIDNotFound: rpctypes.ErrGRPCMemberNotFound,
  29. membership.ErrIDExists: rpctypes.ErrGRPCMemberExist,
  30. membership.ErrPeerURLexists: rpctypes.ErrGRPCPeerURLExist,
  31. etcdserver.ErrNotEnoughStartedMembers: rpctypes.ErrMemberNotEnoughStarted,
  32. mvcc.ErrCompacted: rpctypes.ErrGRPCCompacted,
  33. mvcc.ErrFutureRev: rpctypes.ErrGRPCFutureRev,
  34. etcdserver.ErrRequestTooLarge: rpctypes.ErrGRPCRequestTooLarge,
  35. etcdserver.ErrNoSpace: rpctypes.ErrGRPCNoSpace,
  36. etcdserver.ErrTooManyRequests: rpctypes.ErrTooManyRequests,
  37. etcdserver.ErrNoLeader: rpctypes.ErrGRPCNoLeader,
  38. etcdserver.ErrNotLeader: rpctypes.ErrGRPCNotLeader,
  39. etcdserver.ErrStopped: rpctypes.ErrGRPCStopped,
  40. etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
  41. etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
  42. etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
  43. etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
  44. etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
  45. etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
  46. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  47. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  48. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  49. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  50. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  51. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  52. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  53. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  54. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  55. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  56. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  57. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  58. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  59. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  60. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  61. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  62. }
  63. func togRPCError(err error) error {
  64. // let gRPC server convert to codes.Canceled, codes.DeadlineExceeded
  65. if err == context.Canceled || err == context.DeadlineExceeded {
  66. return err
  67. }
  68. grpcErr, ok := toGRPCErrorMap[err]
  69. if !ok {
  70. return status.Error(codes.Unknown, err.Error())
  71. }
  72. return grpcErr
  73. }
  74. func isClientCtxErr(ctxErr error, err error) bool {
  75. if ctxErr != nil {
  76. return true
  77. }
  78. ev, ok := status.FromError(err)
  79. if !ok {
  80. return false
  81. }
  82. code := ev.Code()
  83. return code == codes.Canceled || code == codes.DeadlineExceeded
  84. }