util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "github.com/coreos/etcd/auth"
  17. "github.com/coreos/etcd/etcdserver"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. "github.com/coreos/etcd/etcdserver/membership"
  20. "github.com/coreos/etcd/lease"
  21. "github.com/coreos/etcd/mvcc"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/codes"
  24. )
  25. var toGRPCErrorMap = map[error]error{
  26. membership.ErrIDRemoved: rpctypes.ErrGRPCMemberNotFound,
  27. membership.ErrIDNotFound: rpctypes.ErrGRPCMemberNotFound,
  28. membership.ErrIDExists: rpctypes.ErrGRPCMemberExist,
  29. membership.ErrPeerURLexists: rpctypes.ErrGRPCPeerURLExist,
  30. etcdserver.ErrNotEnoughStartedMembers: rpctypes.ErrMemberNotEnoughStarted,
  31. mvcc.ErrCompacted: rpctypes.ErrGRPCCompacted,
  32. mvcc.ErrFutureRev: rpctypes.ErrGRPCFutureRev,
  33. etcdserver.ErrRequestTooLarge: rpctypes.ErrGRPCRequestTooLarge,
  34. etcdserver.ErrNoSpace: rpctypes.ErrGRPCNoSpace,
  35. etcdserver.ErrTooManyRequests: rpctypes.ErrTooManyRequests,
  36. etcdserver.ErrNoLeader: rpctypes.ErrGRPCNoLeader,
  37. etcdserver.ErrNotLeader: rpctypes.ErrGRPCNotLeader,
  38. etcdserver.ErrStopped: rpctypes.ErrGRPCStopped,
  39. etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
  40. etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
  41. etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
  42. etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
  43. etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
  44. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  45. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  46. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  47. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  48. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  49. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  50. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  51. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  52. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  53. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  54. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  55. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  56. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  57. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  58. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  59. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  60. }
  61. func togRPCError(err error) error {
  62. grpcErr, ok := toGRPCErrorMap[err]
  63. if !ok {
  64. return grpc.Errorf(codes.Unknown, err.Error())
  65. }
  66. return grpcErr
  67. }