Skip to content

Commit

Permalink
add more tests for fmt.Errorf and improve test function error provide…
Browse files Browse the repository at this point in the history
…r names
  • Loading branch information
Southclaws committed Nov 20, 2022
1 parent f7a1c1f commit 969037c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
22 changes: 22 additions & 0 deletions tests/fault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,25 @@ func TestFlattenStdlibErrorfWrappedError(t *testing.T) {
a.Equal("", e2.Message)
a.Contains(e2.Location, "test_callers.go:11")
}

func TestFlattenStdlibErrorfWrappedExternalError(t *testing.T) {
a := assert.New(t)
err := errorCaller(6)
chain := fault.Flatten(err)

a.ErrorContains(err, "external error wrapped with errorf: stdlib external error")
a.ErrorContains(chain.Root, "stdlib external error")
a.Len(chain.Errors, 3)

e0 := chain.Errors[0]
a.Equal("errorf wrapped external: external error wrapped with errorf: stdlib external error", e0.Message)
a.Contains(e0.Location, "test_callers.go:29")

e1 := chain.Errors[1]
a.Equal("failed to call function", e1.Message)
a.Contains(e1.Location, "test_callers.go:20")

e2 := chain.Errors[2]
a.Equal("", e2.Message)
a.Contains(e2.Location, "test_callers.go:11")
}
9 changes: 9 additions & 0 deletions tests/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func rootCause(kind int) error {
return fault.New("fault root cause error")
} else if kind == 5 {
return fmt.Errorf("errorf wrapped: %w", errSentinelStdlib)
} else if kind == 6 {
err := externalError()
return fmt.Errorf("errorf wrapped external: %w", err)
}
return nil
}

var errExternalStdlib = errors.New("stdlib external error")

func externalError() error {
return fmt.Errorf("external error wrapped with errorf: %w", errExternalStdlib)
}
8 changes: 4 additions & 4 deletions tests/test_callers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import (
)

func errorCaller(kind int) error {
err := errorCallerMid(kind)
err := errorCallerFromMiddleOfChain(kind)
if err != nil {
return fault.Wrap(err)
}

return nil
}

func errorCallerMid(kind int) error {
err := errorCallerDeep(kind)
func errorCallerFromMiddleOfChain(kind int) error {
err := errorProducerFromRootCause(kind)
if err != nil {
return fault.Wrap(err, fmsg.With("failed to call function"))
}

return nil
}

func errorCallerDeep(kind int) error {
func errorProducerFromRootCause(kind int) error {
err := rootCause(kind)
if err != nil {
return fault.Wrap(err)
Expand Down

0 comments on commit 969037c

Please sign in to comment.