Browse Source

rpctypes: Error function to convert clientv3 error

Gyu-Ho Lee 9 years ago
parent
commit
f613052435
1 changed files with 36 additions and 0 deletions
  1. 36 0
      etcdserver/api/v3rpc/rpctypes/error.go

+ 36 - 0
etcdserver/api/v3rpc/rpctypes/error.go

@@ -42,4 +42,40 @@ var (
 	ErrRoleAlreadyExist = grpc.Errorf(codes.FailedPrecondition, "etcdserver: role name already exists")
 	ErrRoleNotFound     = grpc.Errorf(codes.FailedPrecondition, "etcdserver: role name not found")
 	ErrAuthFailed       = grpc.Errorf(codes.InvalidArgument, "etcdserver: authentication failed, invalid user ID or password")
+
+	errStringToError = map[string]error{
+		grpc.ErrorDesc(ErrEmptyKey):     ErrEmptyKey,
+		grpc.ErrorDesc(ErrTooManyOps):   ErrTooManyOps,
+		grpc.ErrorDesc(ErrDuplicateKey): ErrDuplicateKey,
+		grpc.ErrorDesc(ErrCompacted):    ErrCompacted,
+		grpc.ErrorDesc(ErrFutureRev):    ErrFutureRev,
+		grpc.ErrorDesc(ErrNoSpace):      ErrNoSpace,
+
+		grpc.ErrorDesc(ErrLeaseNotFound): ErrLeaseNotFound,
+		grpc.ErrorDesc(ErrLeaseExist):    ErrLeaseExist,
+
+		grpc.ErrorDesc(ErrMemberExist):    ErrMemberExist,
+		grpc.ErrorDesc(ErrPeerURLExist):   ErrPeerURLExist,
+		grpc.ErrorDesc(ErrMemberBadURLs):  ErrMemberBadURLs,
+		grpc.ErrorDesc(ErrMemberNotFound): ErrMemberNotFound,
+
+		grpc.ErrorDesc(ErrRequestTooLarge): ErrRequestTooLarge,
+
+		grpc.ErrorDesc(ErrUserAlreadyExist): ErrUserAlreadyExist,
+		grpc.ErrorDesc(ErrUserNotFound):     ErrUserNotFound,
+		grpc.ErrorDesc(ErrRoleAlreadyExist): ErrRoleAlreadyExist,
+		grpc.ErrorDesc(ErrRoleNotFound):     ErrRoleNotFound,
+		grpc.ErrorDesc(ErrAuthFailed):       ErrAuthFailed,
+	}
 )
+
+func Error(err error) error {
+	if err == nil {
+		return nil
+	}
+	v, ok := errStringToError[err.Error()]
+	if !ok {
+		return err
+	}
+	return v
+}