2023-01-30 21:22:45 +00:00
|
|
|
// 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.
|
2023-02-01 00:01:02 +00:00
|
|
|
type Error struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the error interface.
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:14:36 +00:00
|
|
|
// New returns an error that formats as the given text. Always returns a vizerror.Error.
|
|
|
|
func New(text string) error {
|
2023-02-01 00:01:02 +00:00
|
|
|
return Error{errors.New(text)}
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:14:36 +00:00
|
|
|
// Errorf returns an Error with the specified format and values. Always returns a vizerror.Error.
|
|
|
|
func Errorf(format string, a ...any) error {
|
2023-02-01 00:01:02 +00:00
|
|
|
return Error{fmt.Errorf(format, a...)}
|
|
|
|
}
|
2023-01-30 21:22:45 +00:00
|
|
|
|
2023-02-01 00:01:02 +00:00
|
|
|
// Unwrap returns the underlying error.
|
|
|
|
func (e Error) Unwrap() error {
|
|
|
|
return e.err
|
2023-01-30 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:01:02 +00:00
|
|
|
// Wrap err with a vizerror.Error.
|
2023-02-01 18:14:36 +00:00
|
|
|
func Wrap(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-01 00:01:02 +00:00
|
|
|
return Error{err}
|
2023-01-30 21:22:45 +00:00
|
|
|
}
|