Skip to content

Instantly share code, notes, and snippets.

@erdii
Created September 5, 2023 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erdii/d0e44dd3d3a942f22d47a61111b59cb7 to your computer and use it in GitHub Desktop.
Save erdii/d0e44dd3d3a942f22d47a61111b59cb7 to your computer and use it in GitHub Desktop.
Wrapping multiple errors in go
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