Skip to content

Commit

Permalink
fixes Pass ...Wrapper list to New constructor #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Oct 24, 2023
1 parent 84c038e commit 42fe83a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
10 changes: 8 additions & 2 deletions fundamental.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions fundamental_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 42fe83a

Please sign in to comment.