github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/conn/error_test.go (about)

     1  package conn
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestNodeErrorError(t *testing.T) {
    11  	testErr := errors.New("test")
    12  	nodeErr := newConnError(1, "localhost:1234", testErr)
    13  	message := nodeErr.Error()
    14  
    15  	require.Equal(t, "connError{node_id:1,address:'localhost:1234'}: test", message)
    16  }
    17  
    18  func TestNodeErrorUnwrap(t *testing.T) {
    19  	testErr := errors.New("test")
    20  	nodeErr := newConnError(1, "asd", testErr)
    21  
    22  	unwrapped := errors.Unwrap(nodeErr)
    23  	require.Equal(t, testErr, unwrapped)
    24  }
    25  
    26  func TestNodeErrorIs(t *testing.T) {
    27  	testErr := errors.New("test")
    28  	testErr2 := errors.New("test2")
    29  	nodeErr := newConnError(1, "localhost:1234", testErr)
    30  
    31  	require.ErrorIs(t, nodeErr, testErr)
    32  	require.NotErrorIs(t, nodeErr, testErr2)
    33  }
    34  
    35  type testType1Error struct {
    36  	msg string
    37  }
    38  
    39  func (t testType1Error) Error() string {
    40  	return "1 - " + t.msg
    41  }
    42  
    43  type testType2Error struct {
    44  	msg string
    45  }
    46  
    47  func (t testType2Error) Error() string {
    48  	return "2 - " + t.msg
    49  }
    50  
    51  func TestNodeErrorAs(t *testing.T) {
    52  	testErr := testType1Error{msg: "test"}
    53  	nodeErr := newConnError(1, "localhost:1234", testErr)
    54  
    55  	target := testType1Error{}
    56  	require.ErrorAs(t, nodeErr, &target)
    57  	require.Equal(t, testErr, target)
    58  
    59  	target2 := testType2Error{}
    60  	require.False(t, errors.As(nodeErr, &target2))
    61  }