diff --git a/fundamental.go b/fundamental.go index 2f300f0..89e6b12 100644 --- a/fundamental.go +++ b/fundamental.go @@ -3,12 +3,18 @@ package fault import "fmt" // New creates a new basic fault error. -func New(message string) error { +func New(message string, w ...Wrapper) error { f := &fundamental{ msg: message, location: getLocation(), } - return f + + var err error = f + for _, fn := range w { + err = fn(err) + } + + return err } // Newf includes formatting specifiers. diff --git a/fundamental_test.go b/fundamental_test.go new file mode 100644 index 0000000..6396d0a --- /dev/null +++ b/fundamental_test.go @@ -0,0 +1,39 @@ +package fault + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +type testNewExampleWraper struct{ e error } + +func (e *testNewExampleWraper) Error() string { return "example wrapper" } +func (e *testNewExampleWraper) Unwrap() error { return e.e } + +func testNewExampleWrap() func(error) error { + return func(err error) error { + return &testNewExampleWraper{err} + } +} + +func TestNew(t *testing.T) { + r := require.New(t) + var err error + + err = New("TestNew example") + r.Error(err) + + err = New("TestNew example", testNewExampleWrap()) + r.Error(err) + _, casts := err.(*testNewExampleWraper) + r.True(casts) +} + +func TestNewf(t *testing.T) { + r := require.New(t) + + err := Newf("TestNew example %s", "one") + r.Error(err) + r.Equal("TestNew example one", err.Error()) +}