github.com/koko1123/flow-go-1@v0.29.6/fvm/errors/errors_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  )
    11  
    12  func TestErrorHandling(t *testing.T) {
    13  	require.False(t, IsFailure(nil))
    14  
    15  	t.Run("test nonfatal error detection", func(t *testing.T) {
    16  		e1 := NewOperationNotSupportedError("some operations")
    17  		e2 := fmt.Errorf("some other errors: %w", e1)
    18  		e3 := NewInvalidProposalSignatureError(flow.ProposalKey{}, e2)
    19  		e4 := fmt.Errorf("wrapped: %w", e3)
    20  
    21  		expectedErr := WrapCodedError(
    22  			e1.Code(), // The root cause's error code
    23  			e4,        // All the error message detail.
    24  			"error caused by")
    25  
    26  		txErr, vmErr := SplitErrorTypes(e4)
    27  		require.Nil(t, vmErr)
    28  		require.Equal(t, expectedErr, txErr)
    29  
    30  		require.False(t, IsFailure(e4))
    31  		require.False(t, IsFailure(txErr))
    32  	})
    33  
    34  	t.Run("test fatal error detection", func(t *testing.T) {
    35  		e1 := NewOperationNotSupportedError("some operations")
    36  		e2 := NewEncodingFailuref(e1, "bad encoding")
    37  		e3 := NewLedgerFailure(e2)
    38  		e4 := fmt.Errorf("some other errors: %w", e3)
    39  		e5 := NewInvalidProposalSignatureError(flow.ProposalKey{}, e4)
    40  		e6 := fmt.Errorf("wrapped: %w", e5)
    41  
    42  		expectedErr := WrapCodedError(
    43  			e3.Code(), // The shallowest failure's error code
    44  			e6,        // All the error message detail.
    45  			"failure caused by")
    46  
    47  		txErr, vmErr := SplitErrorTypes(e6)
    48  		require.Nil(t, txErr)
    49  		require.Equal(t, expectedErr, vmErr)
    50  
    51  		require.True(t, IsFailure(e6))
    52  		require.True(t, IsFailure(vmErr))
    53  	})
    54  
    55  	t.Run("unknown error", func(t *testing.T) {
    56  		e1 := fmt.Errorf("some unknown errors")
    57  		txErr, vmErr := SplitErrorTypes(e1)
    58  		require.Nil(t, txErr)
    59  		require.NotNil(t, vmErr)
    60  
    61  		require.True(t, IsFailure(e1))
    62  	})
    63  }