From 62bc1052a28c46f4457a7bab16a6398bdf22b6b5 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Tue, 4 Oct 2022 20:39:10 -0700 Subject: [PATCH] tsweb: allow HTTPError to unwrap errors Signed-off-by: Will Norris --- tsweb/tsweb.go | 2 ++ tsweb/tsweb_test.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/tsweb/tsweb.go b/tsweb/tsweb.go index 9d7c07b3a..38f71cd74 100644 --- a/tsweb/tsweb.go +++ b/tsweb/tsweb.go @@ -421,6 +421,8 @@ type HTTPError struct { // Error implements the error interface. func (e HTTPError) Error() string { return fmt.Sprintf("httperror{%d, %q, %v}", e.Code, e.Msg, e.Err) } +func (e HTTPError) Unwrap() error { return e.Err } + // Error returns an HTTPError containing the given information. func Error(code int, msg string, err error) HTTPError { return HTTPError{Code: code, Msg: msg, Err: err} diff --git a/tsweb/tsweb_test.go b/tsweb/tsweb_test.go index 634486e15..bee029cf7 100644 --- a/tsweb/tsweb_test.go +++ b/tsweb/tsweb_test.go @@ -327,6 +327,14 @@ func BenchmarkLog(b *testing.B) { } } +func TestHTTPError_Unwrap(t *testing.T) { + wrappedErr := fmt.Errorf("wrapped") + err := Error(404, "not found", wrappedErr) + if got := errors.Unwrap(err); got != wrappedErr { + t.Errorf("HTTPError.Unwrap() = %v, want %v", got, wrappedErr) + } +} + func TestVarzHandler(t *testing.T) { t.Run("globals_log", func(t *testing.T) { rec := httptest.NewRecorder()