From 648aa00a283f2a539f663df19f7e5b9863d6455d Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 1 Feb 2023 10:38:45 -0800 Subject: [PATCH] fixup! util/vizerror: add new package for visible errors Signed-off-by: Will Norris --- tsweb/tsweb_test.go | 20 +++++++++++++++++++- util/vizerror/vizerror.go | 6 +++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tsweb/tsweb_test.go b/tsweb/tsweb_test.go index c19c2c5bf..433fa1b7b 100644 --- a/tsweb/tsweb_test.go +++ b/tsweb/tsweb_test.go @@ -153,7 +153,7 @@ func TestStdHandler(t *testing.T) { }, { - name: "handler returns user visible error", + name: "handler returns user-visible error", rh: handlerErr(0, vizerror.New("visible error")), r: req(bgCtx, "http://example.com/foo"), wantCode: 500, @@ -169,6 +169,24 @@ func TestStdHandler(t *testing.T) { }, }, + { + name: "handler returns user-visible error wrapped by private error", + rh: handlerErr(0, fmt.Errorf("private internal error: %w", 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", rh: handlerErr(0, testErr), diff --git a/util/vizerror/vizerror.go b/util/vizerror/vizerror.go index de087ed80..a1268739f 100644 --- a/util/vizerror/vizerror.go +++ b/util/vizerror/vizerror.go @@ -20,12 +20,12 @@ func (e Error) Error() string { return e.err.Error() } -// New returns an error that formats as the given text. Always returns a vizerror.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. Always returns a vizerror.Error. +// 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...)} } @@ -35,7 +35,7 @@ func (e Error) Unwrap() error { return e.err } -// Wrap err with a vizerror.Error. +// Wrap wraps err with a vizerror.Error. func Wrap(err error) error { if err == nil { return nil