util.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. func togRPCError(err error) error {
  26. switch err {
  27. case membership.ErrIDRemoved:
  28. return rpctypes.ErrGRPCMemberNotFound
  29. case membership.ErrIDNotFound:
  30. return rpctypes.ErrGRPCMemberNotFound
  31. case membership.ErrIDExists:
  32. return rpctypes.ErrGRPCMemberExist
  33. case membership.ErrPeerURLexists:
  34. return rpctypes.ErrGRPCPeerURLExist
  35. case mvcc.ErrCompacted:
  36. return rpctypes.ErrGRPCCompacted
  37. case mvcc.ErrFutureRev:
  38. return rpctypes.ErrGRPCFutureRev
  39. case lease.ErrLeaseNotFound:
  40. return rpctypes.ErrGRPCLeaseNotFound
  41. case etcdserver.ErrRequestTooLarge:
  42. return rpctypes.ErrGRPCRequestTooLarge
  43. case etcdserver.ErrNoSpace:
  44. return rpctypes.ErrGRPCNoSpace
  45. case etcdserver.ErrTooManyRequests:
  46. return rpctypes.ErrTooManyRequests
  47. case etcdserver.ErrNoLeader:
  48. return rpctypes.ErrGRPCNoLeader
  49. case etcdserver.ErrStopped:
  50. return rpctypes.ErrGRPCStopped
  51. case etcdserver.ErrTimeout:
  52. return rpctypes.ErrGRPCTimeout
  53. case etcdserver.ErrTimeoutDueToLeaderFail:
  54. return rpctypes.ErrGRPCTimeoutDueToLeaderFail
  55. case auth.ErrRootUserNotExist:
  56. return rpctypes.ErrGRPCRootUserNotExist
  57. case auth.ErrRootRoleNotExist:
  58. return rpctypes.ErrGRPCRootRoleNotExist
  59. case auth.ErrUserAlreadyExist:
  60. return rpctypes.ErrGRPCUserAlreadyExist
  61. case auth.ErrUserNotFound:
  62. return rpctypes.ErrGRPCUserNotFound
  63. case auth.ErrRoleAlreadyExist:
  64. return rpctypes.ErrGRPCRoleAlreadyExist
  65. case auth.ErrRoleNotFound:
  66. return rpctypes.ErrGRPCRoleNotFound
  67. case auth.ErrAuthFailed:
  68. return rpctypes.ErrGRPCAuthFailed
  69. case auth.ErrPermissionDenied:
  70. return rpctypes.ErrGRPCPermissionDenied
  71. case auth.ErrRoleNotGranted:
  72. return rpctypes.ErrGRPCRoleNotGranted
  73. case auth.ErrPermissionNotGranted:
  74. return rpctypes.ErrGRPCPermissionNotGranted
  75. case auth.ErrAuthNotEnabled:
  76. return rpctypes.ErrGRPCAuthNotEnabled
  77. default:
  78. return grpc.Errorf(codes.Unknown, err.Error())
  79. }
  80. }