Skip to content

Commit

Permalink
roachpb: impl errors.SafeFormatter for AmbiguousResultError
Browse files Browse the repository at this point in the history
This is more idiomatic.

Release note: None
  • Loading branch information
tbg committed May 31, 2021
1 parent f6f8e19 commit a20f810
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions pkg/roachpb/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,19 @@ func NewAmbiguousResultErrorf(format string, args ...interface{}) *AmbiguousResu
return NewAmbiguousResultError(errors.Errorf(format, args...))
}

func (e *AmbiguousResultError) SafeFormat(s redact.SafePrinter, _ rune) {
s.SafeString("result is ambiguous: ")
// SafeFormatError implements errors.SafeFormatter.
func (e *AmbiguousResultError) SafeFormatError(p errors.Printer) (next error) {
p.Printf("result is ambiguous")
cause := errors.DecodeError(context.Background(), e.EncodedErr)
s.Print(cause)
if f, l, _, ok := errors.GetOneLineSource(cause); ok {
s.Printf("; originated at %s:%d", redact.SafeString(f), redact.Safe(l))
p.Printf(" (originated at %s:%d)", redact.SafeString(f), redact.Safe(l))
}
return cause
}

// Format implements fmt.Formatter.
func (e *AmbiguousResultError) Format(s fmt.State, v rune) {
errors.FormatError(e, s, v)
}

func (e *AmbiguousResultError) Error() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/roachpb/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestErrorRedaction(t *testing.T) {
act := string(s.RedactableString())
f, l, _, _ := errors.GetOneLineSource(cause)
exp := fmt.Sprintf(
`result is ambiguous: bar: foo 127 ‹secret›; originated at %s:%d`, f, l,
`result is ambiguous (originated at %s:%d)`, f, l,
)
require.Equal(t, exp, act)
})
Expand Down

0 comments on commit a20f810

Please sign in to comment.