github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/irrecoverable/exception_test.go (about)

     1  package irrecoverable
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var sentinelVar = errors.New("sentinelVar")
    12  
    13  type sentinelType struct{}
    14  
    15  func (err sentinelType) Error() string { return "sentinel" }
    16  
    17  func TestWrapSentinelVar(t *testing.T) {
    18  	// wrapping with Errorf should be unwrappable
    19  	err := fmt.Errorf("some error: %w", sentinelVar)
    20  	assert.ErrorIs(t, err, sentinelVar)
    21  
    22  	// wrapping sentinel directly should not be unwrappable
    23  	exception := NewException(sentinelVar)
    24  	assert.NotErrorIs(t, exception, sentinelVar)
    25  
    26  	// wrapping wrapped sentinel should not be unwrappable
    27  	exception = NewException(err)
    28  	assert.NotErrorIs(t, exception, sentinelVar)
    29  }
    30  
    31  func TestWrapSentinelType(t *testing.T) {
    32  	// wrapping with Errorf should be unwrappable
    33  	err := fmt.Errorf("some error: %w", sentinelType{})
    34  	assert.ErrorAs(t, err, &sentinelType{})
    35  
    36  	// wrapping sentinel directly should not be unwrappable
    37  	exception := NewException(sentinelType{})
    38  	assert.False(t, errors.As(exception, &sentinelType{}))
    39  
    40  	// wrapping wrapped sentinel should not be unwrappable
    41  	exception = NewException(err)
    42  	assert.False(t, errors.As(exception, &sentinelType{}))
    43  }