github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/errors_1.13_test.go (about) 1 //go:build go1.13 2 // +build go1.13 3 4 package gin 5 6 import ( 7 "errors" 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 type TestErr string 15 16 func (e TestErr) Error() string { return string(e) } 17 18 // TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()". 19 // "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13, 20 // hence the "// +build go1.13" directive at the beginning of this file. 21 func TestErrorUnwrap(t *testing.T) { 22 innerErr := TestErr("somme error") 23 24 // 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr 25 err := fmt.Errorf("wrapped: %w", &Error{ 26 Err: innerErr, 27 Type: ErrorTypeAny, 28 }) 29 30 // check that 'errors.Is()' and 'errors.As()' behave as expected : 31 assert.True(t, errors.Is(err, innerErr)) 32 var testErr TestErr 33 assert.True(t, errors.As(err, &testErr)) 34 }