mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
fixup! util/vizerror: add new package for visible errors
Signed-off-by: Will Norris <will@tailscale.com>
This commit is contained in:
parent
8e6a1ab175
commit
598ec463bc
@ -32,6 +32,7 @@ import (
|
|||||||
"tailscale.com/net/tsaddr"
|
"tailscale.com/net/tsaddr"
|
||||||
"tailscale.com/types/logger"
|
"tailscale.com/types/logger"
|
||||||
"tailscale.com/util/strs"
|
"tailscale.com/util/strs"
|
||||||
|
"tailscale.com/util/vizerror"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -263,7 +264,16 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
lw := &loggingResponseWriter{ResponseWriter: w, logf: h.opts.Logf}
|
lw := &loggingResponseWriter{ResponseWriter: w, logf: h.opts.Logf}
|
||||||
err := h.rh.ServeHTTPReturn(lw, r)
|
err := h.rh.ServeHTTPReturn(lw, r)
|
||||||
hErr, hErrOK := err.(HTTPError)
|
|
||||||
|
var hErr HTTPError
|
||||||
|
var vizErr vizerror.Error
|
||||||
|
var hErrOK bool
|
||||||
|
if errors.As(err, &hErr) {
|
||||||
|
hErrOK = true
|
||||||
|
} else if errors.As(err, &vizErr) {
|
||||||
|
hErrOK = true
|
||||||
|
hErr = HTTPError{Msg: vizErr.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
if lw.code == 0 && err == nil && !lw.hijacked {
|
if lw.code == 0 && err == nil && !lw.hijacked {
|
||||||
// If the handler didn't write and didn't send a header, that still means 200.
|
// If the handler didn't write and didn't send a header, that still means 200.
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"tailscale.com/metrics"
|
"tailscale.com/metrics"
|
||||||
"tailscale.com/tstest"
|
"tailscale.com/tstest"
|
||||||
|
"tailscale.com/util/vizerror"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -151,6 +152,23 @@ func TestStdHandler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "handler returns user visible error",
|
||||||
|
rh: handlerErr(0, vizerror.New("visible error")),
|
||||||
|
r: req(bgCtx, "http://example.com/foo"),
|
||||||
|
wantCode: 500,
|
||||||
|
wantLog: AccessLogRecord{
|
||||||
|
When: clock.Start,
|
||||||
|
Seconds: 1.0,
|
||||||
|
Proto: "HTTP/1.1",
|
||||||
|
Host: "example.com",
|
||||||
|
Method: "GET",
|
||||||
|
RequestURI: "/foo",
|
||||||
|
Err: "visible error",
|
||||||
|
Code: 500,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "handler returns generic error",
|
name: "handler returns generic error",
|
||||||
rh: handlerErr(0, testErr),
|
rh: handlerErr(0, testErr),
|
||||||
|
@ -11,14 +11,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Error is an error that is safe to display to end users.
|
// Error is an error that is safe to display to end users.
|
||||||
type Error error
|
type Error struct {
|
||||||
|
err 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.
|
// Error implements the error interface.
|
||||||
func Errorf(format string, a ...any) error {
|
func (e Error) Error() string {
|
||||||
return Error(fmt.Errorf(format, a...))
|
return e.err.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...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying error.
|
||||||
|
func (e Error) Unwrap() error {
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap err with a vizerror.Error.
|
||||||
|
func Wrap(err error) Error {
|
||||||
|
return Error{err}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user