Skip to content

Commit

Permalink
Respect TypeModifierOmitStackTrace in Builder
Browse files Browse the repository at this point in the history
Previous implementation of `WithCause` method didn't respect the
`TypeModifierOmitStackTrace` modifier. A stack trace was erroneously
collected if the error that was passed to `WithCause` didn't have
stack trace.
  • Loading branch information
g7r committed Nov 7, 2024
1 parent a1c187a commit b02ead2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func NewErrorBuilder(t *Type) ErrorBuilder {
}

// WithCause provides an original cause for error.
// For non-errorx errors, a stack trace is collected.
// For non-errorx errors, a stack trace is collected unless Type tells otherwise.
// Otherwise, it is inherited by default, as error wrapping is typically performed 'en passe'.
// Note that even if an original error explicitly omitted the stack trace, it could be added on wrap.
func (eb ErrorBuilder) WithCause(err error) ErrorBuilder {
eb.cause = err
if Cast(err) != nil {
if eb.errorType.modifiers.CollectStackTrace() && Cast(err) != nil {
eb.mode = stackTraceBorrow
}

Expand Down
30 changes: 30 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,33 @@ func TestBuilderTransparency(t *testing.T) {
require.NotEqual(t, testType, err.Type())
})
}

func TestBuilderRespectsNoStackTrace(t *testing.T) {
wrapperErrorTypes := []*Type{testTypeSilent, testTypeSilentTransparent}

for _, et := range wrapperErrorTypes {
t.Run(et.String(), func(t *testing.T) {
t.Run("Naked", func(t *testing.T) {
err := NewErrorBuilder(et).
WithCause(errors.New("naked error")).
Create()
require.Nil(t, err.stackTrace)
})

t.Run("WithoutStacktrace", func(t *testing.T) {
err := NewErrorBuilder(et).
WithCause(testTypeSilent.NewWithNoMessage()).
Create()
require.Nil(t, err.stackTrace)
})

t.Run("WithStacktrace", func(t *testing.T) {
err := NewErrorBuilder(et).
WithCause(testType.NewWithNoMessage()).
Create()
require.Nil(t, err.stackTrace)
})
})
}

}
17 changes: 9 additions & 8 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

var (
testNamespace = NewNamespace("foo")
testType = testNamespace.NewType("bar")
testTypeSilent = testType.NewSubtype("silent").ApplyModifiers(TypeModifierOmitStackTrace)
testTypeTransparent = testType.NewSubtype("transparent").ApplyModifiers(TypeModifierTransparent)
testSubtype0 = testType.NewSubtype("internal")
testSubtype1 = testSubtype0.NewSubtype("wat")
testTypeBar1 = testNamespace.NewType("bar1")
testTypeBar2 = testNamespace.NewType("bar2")
testNamespace = NewNamespace("foo")
testType = testNamespace.NewType("bar")
testTypeSilent = testType.NewSubtype("silent").ApplyModifiers(TypeModifierOmitStackTrace)
testTypeTransparent = testType.NewSubtype("transparent").ApplyModifiers(TypeModifierTransparent)
testTypeSilentTransparent = testType.NewSubtype("silent_transparent").ApplyModifiers(TypeModifierTransparent, TypeModifierOmitStackTrace)
testSubtype0 = testType.NewSubtype("internal")
testSubtype1 = testSubtype0.NewSubtype("wat")
testTypeBar1 = testNamespace.NewType("bar1")
testTypeBar2 = testNamespace.NewType("bar2")
)

func TestError(t *testing.T) {
Expand Down

0 comments on commit b02ead2

Please sign in to comment.