util.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
  45. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  46. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  47. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  48. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  49. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  50. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  51. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  52. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  53. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  54. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  55. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  56. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  57. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  58. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  59. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  60. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  61. }
  62. func togRPCError(err error) error {
  63. grpcErr, ok := toGRPCErrorMap[err]
  64. if !ok {
  65. return grpc.Errorf(codes.Unknown, err.Error())
  66. }
  67. return grpcErr
  68. }