Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add causal chain to the stack string #52

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,28 @@ func (p *Error) StackTrace() []uintptr {
return out
}

// StackString formats the stack as a beautiful string with newlines
// StackString formats the stacks from the terror chain as a string. If we
// encounter more than one terror in the chain with a stack frame, we'll print
// each one, separated by three hyphens on their own line.
func (p *Error) StackString() string {
stackStr := ""
for _, frame := range p.StackFrames {
stackStr = fmt.Sprintf("%s\n %s:%d in %s", stackStr, frame.Filename, frame.Line, frame.Method)
var buffer strings.Builder
terr := p
for terr != nil {
if buffer.Len() != 0 && len(terr.StackFrames) > 0 {
fmt.Fprintf(&buffer, "\n---")
}
for _, frame := range terr.StackFrames {
fmt.Fprintf(&buffer, "\n %s:%d in %s", frame.Filename, frame.Line, frame.Method)
}

if tcause, ok := terr.cause.(*Error); ok {
terr = tcause
} else {
break
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding some comments here or updating the function comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do you think we need to worry about the performance here if we have a really long call stack? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't quite sure what you were going for with the comments, so I've just followed the inferred intent of the original.

And for performance; granted, there is a risk of poor performance in extreme circumstances. But on the other hand, I feel like the usage of strings.Buffer over repeated use of fmt.Sprintf() is enough of a net win to outweigh any downside in most circumstances.

}
return stackStr

return buffer.String()
}

// VerboseString returns the error message, stack trace and params
Expand Down
12 changes: 12 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,15 @@ func TestSetIsUnexpected(t *testing.T) {
err.SetIsUnexpected(false)
assert.False(t, *err.IsUnexpected)
}

func failyFunction() error {
return InternalService("halp", "I'm in trouble", nil)
}

func TestStackStringChasesCausalChain(t *testing.T) {
err := Augment(failyFunction(), "something may be up", nil)
terr := err.(*Error)
ss := terr.StackString()
t.Log(ss)
assert.Contains(t, ss, "failyFunction")
}
Loading