Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ outer:
return buffer.String()
}

// MessageChainString returns a formatted string of the full message chain.
// Each message in the chain is joined with " -> ".
func (p *Error) MessageChainString() string {
if p == nil {
return ""
}
return strings.Join(p.MessageChain, " -> ")
}

// VerboseString returns the error message, stack trace and params
func (p *Error) VerboseString() string {
return fmt.Sprintf("%s\nParams: %+v\n%s", p.Error(), p.Params, p.StackString())
Expand Down
25 changes: 25 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,31 @@ func TestStackStringChasesCausalChain(t *testing.T) {
assert.Contains(t, ss, "failyFunction")
}

func TestMessageChainString(t *testing.T) {
t.Run("simple chain", func(t *testing.T) {
err := &Error{
Code: ErrInternalService,
MessageChain: []string{"top-level", "cause"},
}
out := err.MessageChainString()
assert.Equal(t, "top-level -> cause", out)
})

t.Run("single message", func(t *testing.T) {
err := &Error{
Code: ErrNotFound,
MessageChain: []string{"only one"},
}
out := err.MessageChainString()
assert.Equal(t, "only one", out)
})

t.Run("nil error returns empty string", func(t *testing.T) {
var err *Error
assert.Equal(t, "", err.MessageChainString())
})
}

func TestCircularErrorProducesFiniteOutputWithStackFrames(t *testing.T) {
orig := failyFunction()
err := Augment(orig, "something may be up", nil)
Expand Down