From a36fe24b8383f6cd9b3519cd3eabdb9675d8992d Mon Sep 17 00:00:00 2001 From: Taras Madan Date: Fri, 21 Jul 2023 11:51:35 +0200 Subject: all: use errors.As instead of .(type) --- dashboard/app/access_test.go | 5 +++-- dashboard/app/app_test.go | 9 +++++---- dashboard/app/bisect_test.go | 5 +++-- dashboard/app/handler.go | 5 +++-- dashboard/app/reporting_email.go | 14 ++++++++------ dashboard/app/reporting_external.go | 4 +++- dashboard/app/util_test.go | 5 +++-- 7 files changed, 28 insertions(+), 19 deletions(-) (limited to 'dashboard') diff --git a/dashboard/app/access_test.go b/dashboard/app/access_test.go index f7d02d990..7bd412acf 100644 --- a/dashboard/app/access_test.go +++ b/dashboard/app/access_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "errors" "fmt" "net/http" "strconv" @@ -387,8 +388,8 @@ func TestAccess(t *testing.T) { t.Fatal(err1) } c.expectNE(err, nil) - httpErr, ok := err.(*HTTPError) - c.expectTrue(ok) + var httpErr *HTTPError + c.expectTrue(errors.As(err, &httpErr)) c.expectEQ(httpErr.Code, http.StatusTemporaryRedirect) c.expectEQ(httpErr.Headers["Location"], []string{loginURL}) } else { diff --git a/dashboard/app/app_test.go b/dashboard/app/app_test.go index d7aae91ed..241e5141d 100644 --- a/dashboard/app/app_test.go +++ b/dashboard/app/app_test.go @@ -4,6 +4,7 @@ package main import ( + "errors" "fmt" "net/http" "os" @@ -820,8 +821,8 @@ func checkLoginRedirect(c *Ctx, accessLevel AccessLevel, url string) { func checkRedirect(c *Ctx, accessLevel AccessLevel, from, to string, status int) { _, err := c.AuthGET(accessLevel, from) c.expectNE(err, nil) - httpErr, ok := err.(*HTTPError) - c.expectTrue(ok) + var httpErr *HTTPError + c.expectTrue(errors.As(err, &httpErr)) c.expectEQ(httpErr.Code, status) c.expectEQ(httpErr.Headers["Location"], []string{to}) } @@ -829,8 +830,8 @@ func checkRedirect(c *Ctx, accessLevel AccessLevel, from, to string, status int) func checkResponseStatusCode(c *Ctx, accessLevel AccessLevel, url string, status int) { _, err := c.AuthGET(accessLevel, url) c.expectNE(err, nil) - httpErr, ok := err.(*HTTPError) - c.expectTrue(ok) + var httpErr *HTTPError + c.expectTrue(errors.As(err, &httpErr)) c.expectEQ(httpErr.Code, status) } diff --git a/dashboard/app/bisect_test.go b/dashboard/app/bisect_test.go index a61f51286..334d5a04d 100644 --- a/dashboard/app/bisect_test.go +++ b/dashboard/app/bisect_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "errors" "fmt" "net/http" "strings" @@ -1106,8 +1107,8 @@ func TestBugBisectionInvalidation(t *testing.T) { // Mark bisection as invalid. _, err = c.AuthGET(AccessAdmin, "/admin?action=invalidate_bisection&key="+jobKey.Encode()) - httpErr, ok := err.(*HTTPError) - c.expectTrue(ok) + var httpErr *HTTPError + c.expectTrue(errors.As(err, &httpErr)) c.expectEQ(httpErr.Code, http.StatusFound) // The invalidated bisection should have vanished from the web UI diff --git a/dashboard/app/handler.go b/dashboard/app/handler.go index 6a115d0b2..6b72da0bb 100644 --- a/dashboard/app/handler.go +++ b/dashboard/app/handler.go @@ -50,7 +50,8 @@ func handleContext(fn contextHandler) http.Handler { http.Error(w, "403 Forbidden", http.StatusForbidden) return } - if redir, ok := err.(ErrRedirect); ok { + var redir *ErrRedirect + if errors.As(err, &redir) { http.Redirect(w, r, redir.Error(), http.StatusFound) return } @@ -201,7 +202,7 @@ func commonHeader(c context.Context, r *http.Request, w http.ResponseWriter, ns ns = adminPage } if ns != adminPage || !isAdminPage { - return nil, ErrRedirect{fmt.Errorf("/%v", ns)} + return nil, &ErrRedirect{fmt.Errorf("/%v", ns)} } } if ns != adminPage { diff --git a/dashboard/app/reporting_email.go b/dashboard/app/reporting_email.go index ee87169b7..26a3e57ef 100644 --- a/dashboard/app/reporting_email.go +++ b/dashboard/app/reporting_email.go @@ -703,17 +703,19 @@ func handleTestCommand(c context.Context, info *bugInfoResult, user: msg.Author, extID: msg.MessageID, link: msg.Link, patch: []byte(msg.Patch), repo: args[0], branch: args[1], jobCC: msg.Cc}) if err != nil { - switch e := err.(type) { - case *TestRequestDeniedError: + var testDenied *TestRequestDeniedError + var badTest *BadTestRequestError + switch { + case errors.As(err, &testDenied): // Don't send a reply in this case. - log.Errorf(c, "patch test request denied: %v", e) - case *BadTestRequestError: - reply = e.Error() + log.Errorf(c, "patch test request denied: %v", testDenied) + case errors.As(err, &badTest): + reply = badTest.Error() default: // Don't leak any details to the reply email. reply = "Processing failed due to an internal error" // .. but they are useful for debugging, so we'd like to see it on the Admin page. - log.Errorf(c, "handleTestRequest error: %v", e) + log.Errorf(c, "handleTestRequest error: %v", err) } } return reply diff --git a/dashboard/app/reporting_external.go b/dashboard/app/reporting_external.go index ffe3f00c8..b296139ec 100644 --- a/dashboard/app/reporting_external.go +++ b/dashboard/app/reporting_external.go @@ -5,6 +5,7 @@ package main import ( "encoding/json" + "errors" "fmt" "net/http" @@ -95,7 +96,8 @@ func apiNewTestJob(c context.Context, r *http.Request, payload []byte) (interfac err := handleExternalTestRequest(c, req) if err != nil { resp.ErrorText = err.Error() - if _, ok := err.(*BadTestRequestError); !ok { + var badTest *BadTestRequestError + if !errors.As(err, &badTest) { // Log errors that are not related to the invalid input. log.Errorf(c, "external patch posting error: %v", err) } diff --git a/dashboard/app/util_test.go b/dashboard/app/util_test.go index f748e0dcd..b0ac1c6fe 100644 --- a/dashboard/app/util_test.go +++ b/dashboard/app/util_test.go @@ -8,6 +8,7 @@ package main import ( "bytes" + "errors" "fmt" "io" "net/http" @@ -107,8 +108,8 @@ func (c *Ctx) expectFailureStatus(err error, code int) { if err == nil { c.t.Fatalf("expected to fail as %d, but it does not", code) } - httpErr, ok := err.(*HTTPError) - if !ok || httpErr.Code != code { + var httpErr *HTTPError + if !errors.As(err, &httpErr) || httpErr.Code != code { c.t.Fatalf("expected to fail as %d, but it failed as %v", code, err) } } -- cgit mrf-deployment