Skip to content

Commit

Permalink
Keep cause stack trace even with TypeModifierOmitStackTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
g7r committed Nov 8, 2024
1 parent b02ead2 commit f1da37c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
35 changes: 22 additions & 13 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ func NewErrorBuilder(t *Type) ErrorBuilder {
// 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 eb.errorType.modifiers.CollectStackTrace() && Cast(err) != nil {
eb.mode = stackTraceBorrow
if Cast(err) != nil {
if eb.errorType.modifiers.CollectStackTrace() {
eb.mode = stackTraceBorrowOrCollect
} else {
eb.mode = stackTraceBorrowOnly
}
}

return eb
Expand Down Expand Up @@ -105,18 +109,27 @@ func (eb ErrorBuilder) Create() *Error {
type callStackBuildMode int

const (
stackTraceCollect callStackBuildMode = 1
stackTraceBorrow callStackBuildMode = 2
stackTraceEnhance callStackBuildMode = 3
stackTraceOmit callStackBuildMode = 4
stackTraceCollect callStackBuildMode = 1
stackTraceBorrowOrCollect callStackBuildMode = 2
stackTraceBorrowOnly callStackBuildMode = 3
stackTraceEnhance callStackBuildMode = 4
stackTraceOmit callStackBuildMode = 5
)

func (eb ErrorBuilder) assembleStackTrace() *stackTrace {
switch eb.mode {
case stackTraceCollect:
return eb.collectOriginalStackTrace()
case stackTraceBorrow:
return eb.borrowStackTraceFromCause()
case stackTraceBorrowOnly, stackTraceBorrowOrCollect:
if st := eb.borrowStackTraceFromCause(); st != nil {
return st
}

if eb.mode == stackTraceBorrowOrCollect {
return collectStackTrace()
}

return nil
case stackTraceEnhance:
return eb.combineStackTraceWithCause()
case stackTraceOmit:
Expand All @@ -131,11 +144,7 @@ func (eb ErrorBuilder) collectOriginalStackTrace() *stackTrace {
}

func (eb ErrorBuilder) borrowStackTraceFromCause() *stackTrace {
originalStackTrace := eb.extractStackTraceFromCause(eb.cause)
if originalStackTrace != nil {
return originalStackTrace
}
return collectStackTrace()
return eb.extractStackTraceFromCause(eb.cause)
}

func (eb ErrorBuilder) combineStackTraceWithCause() *stackTrace {
Expand Down
12 changes: 9 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errorx

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -21,6 +22,10 @@ func TestBuilderTransparency(t *testing.T) {
})
}

func testBuilderRespectsNoStackTraceMarkerFrame() error {
return testType.NewWithNoMessage()
}

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

Expand All @@ -41,12 +46,13 @@ func TestBuilderRespectsNoStackTrace(t *testing.T) {
})

t.Run("WithStacktrace", func(t *testing.T) {
cause := testBuilderRespectsNoStackTraceMarkerFrame()
err := NewErrorBuilder(et).
WithCause(testType.NewWithNoMessage()).
WithCause(cause).
Create()
require.Nil(t, err.stackTrace)
require.Same(t, err.stackTrace, Cast(cause).stackTrace)
require.Contains(t, fmt.Sprintf("%+v", err), "testBuilderRespectsNoStackTraceMarkerFrame")
})
})
}

}
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ module github.com/joomcode/errorx

go 1.11

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
require github.com/stretchr/testify v1.9.0
17 changes: 15 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit f1da37c

Please sign in to comment.