util/vizerror: add WrapWithMessage

Thus new function allows constructing vizerrors that combine a message
appropriate for display to users with a wrapped underlying error.

Updates tailscale/corp#23781

Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:
Percy Wegmann
2024-10-09 12:05:33 -05:00
committed by Percy Wegmann
parent 910b4e8e6a
commit 2cadb80fb2
2 changed files with 67 additions and 13 deletions

View File

@ -42,3 +42,25 @@ func TestAs(t *testing.T) {
t.Errorf("As() returned error %v, want %v", got, verr)
}
}
func TestWrap(t *testing.T) {
wrapped := errors.New("wrapped")
err := Wrap(wrapped)
if err.Error() != "wrapped" {
t.Errorf(`Wrap(wrapped).Error() = %q, want %q`, err.Error(), "wrapped")
}
if errors.Unwrap(err) != wrapped {
t.Errorf("Unwrap = %q, want %q", errors.Unwrap(err), wrapped)
}
}
func TestWrapWithMessage(t *testing.T) {
wrapped := errors.New("wrapped")
err := WrapWithMessage(wrapped, "safe")
if err.Error() != "safe" {
t.Errorf(`WrapWithMessage(wrapped, "safe").Error() = %q, want %q`, err.Error(), "safe")
}
if errors.Unwrap(err) != wrapped {
t.Errorf("Unwrap = %q, want %q", errors.Unwrap(err), wrapped)
}
}