Created
September 5, 2023 22:32
-
-
Save erdii/d0e44dd3d3a942f22d47a61111b59cb7 to your computer and use it in GitHub Desktop.
Wrapping multiple errors in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package playground | |
import ( | |
"errors" | |
"fmt" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
var ( | |
ErrOne = errors.New("error one") | |
ErrTwo = errors.New("error two") | |
ErrThree = errors.New("error three") | |
) | |
func TestErrMultiWrap(t *testing.T) { | |
wrapper := fmt.Errorf("%w %w", ErrOne, ErrTwo) | |
assert.ErrorIs(t, wrapper, ErrOne) | |
assert.ErrorIs(t, wrapper, ErrTwo) | |
assert.NotErrorIs(t, wrapper, ErrThree) | |
{ | |
withThree := fmt.Errorf("%w %w", wrapper, ErrThree) | |
assert.ErrorIs(t, withThree, ErrOne) | |
assert.ErrorIs(t, withThree, ErrTwo) | |
assert.ErrorIs(t, withThree, ErrThree) | |
} | |
{ // Let's make sure that shenanigans don't break it. | |
withDuplicate := fmt.Errorf("%w %w", wrapper, ErrOne) | |
assert.ErrorIs(t, withDuplicate, ErrOne) | |
assert.ErrorIs(t, withDuplicate, ErrTwo) | |
assert.NotErrorIs(t, withDuplicate, ErrThree) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment