mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-21 06:01:42 +00:00
util/vizerror: add new package for visible errors
Signed-off-by: Will Norris <will@tailscale.com>
This commit is contained in:
parent
27d146d4f8
commit
8e6a1ab175
24
util/vizerror/vizerror.go
Normal file
24
util/vizerror/vizerror.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
// Package vizerror provides types and utility funcs for handling visible errors
|
||||||
|
// that are safe to display to end users.
|
||||||
|
package vizerror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error is an error that is safe to display to end users.
|
||||||
|
type Error error
|
||||||
|
|
||||||
|
// New returns an error that formats as the given text.
|
||||||
|
func New(text string) error {
|
||||||
|
return Error(errors.New(text))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf returns an error with the specified format and values.
|
||||||
|
func Errorf(format string, a ...any) error {
|
||||||
|
return Error(fmt.Errorf(format, a...))
|
||||||
|
}
|
30
util/vizerror/vizerror_test.go
Normal file
30
util/vizerror/vizerror_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
package vizerror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
err := New("abc")
|
||||||
|
if err.Error() != "abc" {
|
||||||
|
t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorf(t *testing.T) {
|
||||||
|
err := Errorf("%w", fs.ErrNotExist)
|
||||||
|
|
||||||
|
if got, want := err.Error(), "file does not exist"; got != want {
|
||||||
|
t.Errorf("Errorf().Error() = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure error wrapping still works
|
||||||
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
|
t.Errorf("error chain does not contain fs.ErrNotExist")
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user