go.etcd.io/etcd@v3.3.27+incompatible/etcdserver/api/v3rpc/util_test.go (about)

     1  // Copyright 2017 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  
    15  package v3rpc
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
    23  	"github.com/coreos/etcd/mvcc"
    24  
    25  	"google.golang.org/grpc/codes"
    26  	"google.golang.org/grpc/status"
    27  )
    28  
    29  func TestGRPCError(t *testing.T) {
    30  	tt := []struct {
    31  		err error
    32  		exp error
    33  	}{
    34  		{err: mvcc.ErrCompacted, exp: rpctypes.ErrGRPCCompacted},
    35  		{err: mvcc.ErrFutureRev, exp: rpctypes.ErrGRPCFutureRev},
    36  		{err: context.Canceled, exp: context.Canceled},
    37  		{err: context.DeadlineExceeded, exp: context.DeadlineExceeded},
    38  		{err: errors.New("foo"), exp: status.Error(codes.Unknown, "foo")},
    39  	}
    40  	for i := range tt {
    41  		if err := togRPCError(tt[i].err); err != tt[i].exp {
    42  			if _, ok := status.FromError(err); ok {
    43  				if err.Error() == tt[i].exp.Error() {
    44  					continue
    45  				}
    46  			}
    47  			t.Errorf("#%d: got %v, expected %v", i, err, tt[i].exp)
    48  		}
    49  	}
    50  }