mirror of
https://github.com/tailscale/tailscale.git
synced 2025-03-27 03:22:25 +00:00

Updates tailscale/corp#23781 Change-Id: I072d0169703aefb0f37b1fa715881f1ccb561f03 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
// 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 struct {
|
|
err error
|
|
}
|
|
|
|
// Error implements the error interface.
|
|
func (e Error) Error() string {
|
|
return e.err.Error()
|
|
}
|
|
|
|
// New returns an error that formats as the given text. It always returns a vizerror.Error.
|
|
func New(text string) error {
|
|
return Error{errors.New(text)}
|
|
}
|
|
|
|
// Errorf returns an Error with the specified format and values. It always returns a vizerror.Error.
|
|
func Errorf(format string, a ...any) error {
|
|
return Error{fmt.Errorf(format, a...)}
|
|
}
|
|
|
|
// Unwrap returns the underlying error.
|
|
func (e Error) Unwrap() error {
|
|
return e.err
|
|
}
|
|
|
|
// Wrap wraps err with a vizerror.Error.
|
|
func Wrap(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return Error{err}
|
|
}
|
|
|
|
// As returns the first vizerror.Error in err's chain.
|
|
func As(err error) (e Error, ok bool) {
|
|
ok = errors.As(err, &e)
|
|
return
|
|
}
|
|
|
|
// ErrorWithInternal is a tuple of a user-visible error along with
|
|
// an internal error that should not be shown to users.
|
|
type ErrorWithInternal struct {
|
|
UserVisibleErr Error // user-visible half
|
|
InternalErr error // internal error not to be shown to users
|
|
}
|
|
|
|
func (e ErrorWithInternal) Error() string {
|
|
return e.UserVisibleErr.Error()
|
|
}
|
|
|
|
// WithInternal returns a new ErrorWithInternal combining a user-visible error
|
|
// string and an internal error to pass around for internal logging but not
|
|
// to be shown to end users.
|
|
func WithInternal(visibleError string, internalErr error) error {
|
|
return ErrorWithInternal{Error{errors.New(visibleError)}, internalErr}
|
|
}
|