From c56abfb840b6483952b95ac6d6e6cac6a141b90e Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Fri, 15 Dec 2023 13:21:54 -0600 Subject: [PATCH] Enable usestdlibvars linter and fix findings (#13339) Co-authored-by: terence --- .golangci.yml | 1 + api/client/builder/client.go | 8 ++++---- api/client/errors.go | 2 +- testing/endtoend/components/web3remotesigner.go | 6 +++--- testing/endtoend/evaluators/beaconapi/util.go | 2 +- validator/web/handler.go | 6 +++--- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7fbfeac472..c18c3c3fc8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,6 +21,7 @@ linters: - nilerr - whitespace - misspell + - usestdlibvars - errname linters-settings: diff --git a/api/client/builder/client.go b/api/client/builder/client.go index 7a477b4e7d..aa6d2c7524 100644 --- a/api/client/builder/client.go +++ b/api/client/builder/client.go @@ -423,22 +423,22 @@ func non200Err(response *http.Response) error { } msg := fmt.Sprintf("code=%d, url=%s, body=%s", response.StatusCode, response.Request.URL, body) switch response.StatusCode { - case 204: + case http.StatusNoContent: log.WithError(ErrNoContent).Debug(msg) return ErrNoContent - case 400: + case http.StatusBadRequest: if jsonErr := json.Unmarshal(bodyBytes, &errMessage); jsonErr != nil { return errors.Wrap(jsonErr, "unable to read response body") } log.WithError(ErrBadRequest).Debug(msg) return errors.Wrap(ErrBadRequest, errMessage.Message) - case 404: + case http.StatusNotFound: if jsonErr := json.Unmarshal(bodyBytes, &errMessage); jsonErr != nil { return errors.Wrap(jsonErr, "unable to read response body") } log.WithError(ErrNotFound).Debug(msg) return errors.Wrap(ErrNotFound, errMessage.Message) - case 500: + case http.StatusInternalServerError: if jsonErr := json.Unmarshal(bodyBytes, &errMessage); jsonErr != nil { return errors.Wrap(jsonErr, "unable to read response body") } diff --git a/api/client/errors.go b/api/client/errors.go index 667728081d..fb4ef3ae0f 100644 --- a/api/client/errors.go +++ b/api/client/errors.go @@ -32,7 +32,7 @@ func Non200Err(response *http.Response) error { } msg := fmt.Sprintf("code=%d, url=%s, body=%s", response.StatusCode, response.Request.URL, body) switch response.StatusCode { - case 404: + case http.StatusNotFound: return errors.Wrap(ErrNotFound, msg) default: return errors.Wrap(ErrNotOK, msg) diff --git a/testing/endtoend/components/web3remotesigner.go b/testing/endtoend/components/web3remotesigner.go index fe3c36357c..d93576b32b 100644 --- a/testing/endtoend/components/web3remotesigner.go +++ b/testing/endtoend/components/web3remotesigner.go @@ -142,7 +142,7 @@ func (w *Web3RemoteSigner) Stop() error { func (w *Web3RemoteSigner) monitorStart() { client := &http.Client{} for { - req, err := http.NewRequestWithContext(w.ctx, "GET", fmt.Sprintf("http://localhost:%d/upcheck", Web3RemoteSignerPort), nil) + req, err := http.NewRequestWithContext(w.ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d/upcheck", Web3RemoteSignerPort), nil) if err != nil { panic(err) } @@ -172,7 +172,7 @@ func (w *Web3RemoteSigner) PublicKeys(ctx context.Context) ([]bls.PublicKey, err w.wait(ctx) client := &http.Client{} - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://localhost:%d/api/v1/eth2/publicKeys", Web3RemoteSignerPort), nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d/api/v1/eth2/publicKeys", Web3RemoteSignerPort), nil) if err != nil { return nil, err } @@ -180,7 +180,7 @@ func (w *Web3RemoteSigner) PublicKeys(ctx context.Context) ([]bls.PublicKey, err if err != nil { return nil, err } - if res.StatusCode != 200 { + if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("returned status code %d", res.StatusCode) } b, err := io.ReadAll(res.Body) diff --git a/testing/endtoend/evaluators/beaconapi/util.go b/testing/endtoend/evaluators/beaconapi/util.go index 9e77318b82..d2d9a7abd7 100644 --- a/testing/endtoend/evaluators/beaconapi/util.go +++ b/testing/endtoend/evaluators/beaconapi/util.go @@ -83,7 +83,7 @@ func doSSZGetRequest(template string, requestPath string, beaconNodeIdx int, bnT basePath := fmt.Sprintf(template, port+beaconNodeIdx) - req, err := http.NewRequest("GET", basePath+requestPath, nil) + req, err := http.NewRequest(http.MethodGet, basePath+requestPath, nil) if err != nil { return nil, err } diff --git a/validator/web/handler.go b/validator/web/handler.go index ad188a0d78..ac9ad89d30 100644 --- a/validator/web/handler.go +++ b/validator/web/handler.go @@ -27,7 +27,7 @@ var Handler = func(res http.ResponseWriter, req *http.Request) { if d, ok := _bindata[p]; ok { m := mime.TypeByExtension(path.Ext(p)) res.Header().Add("Content-Type", m) - res.WriteHeader(200) + res.WriteHeader(http.StatusOK) asset, err := d() if err != nil { log.WithError(err).Error("Failed to unwrap asset data for http response") @@ -40,7 +40,7 @@ var Handler = func(res http.ResponseWriter, req *http.Request) { // requesting /login, this should serve the single page app index.html. m := mime.TypeByExtension(".html") res.Header().Add("Content-Type", m) - res.WriteHeader(200) + res.WriteHeader(http.StatusOK) asset, err := d() if err != nil { log.WithError(err).Error("Failed to unwrap asset data for http response") @@ -50,7 +50,7 @@ var Handler = func(res http.ResponseWriter, req *http.Request) { } } else { // If index.html is not present, serve 404. This should never happen. log.WithField("URI", req.RequestURI).Error("Path not found") - res.WriteHeader(404) + res.WriteHeader(http.StatusNotFound) if _, err := res.Write([]byte("Not found")); err != nil { log.WithError(err).Error("Failed to write http response") }