Skip to content
Merged
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
10 changes: 10 additions & 0 deletions pkg/errox/roxerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,13 @@ func (e *RoxError) Newf(format string, args ...interface{}) *RoxError {
func (e *RoxError) CausedBy(cause interface{}) error {
return fmt.Errorf("%w: %v", e, cause)
}

// CausedByf adds a cause to the RoxError. The resulting message is a
// combination of the rox error and the cause message, formatted based on the
// provided format specifier and arguments, following a colon.
//
// Example:
// return errox.InvalidArgument.CausedByf("unknown parameter %v", p)
func (e *RoxError) CausedByf(format string, args ...interface{}) error {
return e.CausedBy(fmt.Sprintf(format, args...))
}
6 changes: 6 additions & 0 deletions pkg/errox/roxerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ func TestCausedBy(t *testing.T) {
err := NotFound.New("lost forever").CausedBy("swallowed by Kraken")
assert.ErrorIs(t, err, NotFound)
}

{
err := NotFound.New("absolute disaster").CausedByf("out of %v", "sense")
assert.Equal(t, "absolute disaster: out of sense", err.Error())
assert.ErrorIs(t, err, NotFound)
}
}