Chore: Change fmt.Errorf to errors.New when there is no formatting required (#58600)

Signed-off-by: Sasha Melentyev <sasha@melentyev.io>
This commit is contained in:
Sasha Melentyev 2022-12-01 22:51:12 +03:00 committed by GitHub
parent fb98a97efa
commit f0adc69ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 25 deletions

View File

@ -490,11 +490,11 @@ func (hs *HTTPServer) getListener() (net.Listener, error) {
func (hs *HTTPServer) configureHttps() error { func (hs *HTTPServer) configureHttps() error {
if hs.Cfg.CertFile == "" { if hs.Cfg.CertFile == "" {
return fmt.Errorf("cert_file cannot be empty when using HTTPS") return errors.New("cert_file cannot be empty when using HTTPS")
} }
if hs.Cfg.KeyFile == "" { if hs.Cfg.KeyFile == "" {
return fmt.Errorf("cert_key cannot be empty when using HTTPS") return errors.New("cert_key cannot be empty when using HTTPS")
} }
if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) { if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) {
@ -530,19 +530,19 @@ func (hs *HTTPServer) configureHttps() error {
func (hs *HTTPServer) configureHttp2() error { func (hs *HTTPServer) configureHttp2() error {
if hs.Cfg.CertFile == "" { if hs.Cfg.CertFile == "" {
return fmt.Errorf("cert_file cannot be empty when using HTTP2") return errors.New("cert_file cannot be empty when using HTTP2")
} }
if hs.Cfg.KeyFile == "" { if hs.Cfg.KeyFile == "" {
return fmt.Errorf("cert_key cannot be empty when using HTTP2") return errors.New("cert_key cannot be empty when using HTTP2")
} }
if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) { if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) {
return fmt.Errorf(`cannot find SSL cert_file at %q`, hs.Cfg.CertFile) return fmt.Errorf("cannot find SSL cert_file at %q", hs.Cfg.CertFile)
} }
if _, err := os.Stat(hs.Cfg.KeyFile); os.IsNotExist(err) { if _, err := os.Stat(hs.Cfg.KeyFile); os.IsNotExist(err) {
return fmt.Errorf(`cannot find SSL key_file at %q`, hs.Cfg.KeyFile) return fmt.Errorf("cannot find SSL key_file at %q", hs.Cfg.KeyFile)
} }
tlsCfg := &tls.Config{ tlsCfg := &tls.Config{
@ -664,9 +664,8 @@ func (hs *HTTPServer) healthzHandler(ctx *web.Context) {
return return
} }
ctx.Resp.WriteHeader(200) ctx.Resp.WriteHeader(http.StatusOK)
_, err := ctx.Resp.Write([]byte("Ok")) if _, err := ctx.Resp.Write([]byte("Ok")); err != nil {
if err != nil {
hs.log.Error("could not write to response", "err", err) hs.log.Error("could not write to response", "err", err)
} }
} }
@ -690,10 +689,10 @@ func (hs *HTTPServer) apiHealthHandler(ctx *web.Context) {
if !hs.databaseHealthy(ctx.Req.Context()) { if !hs.databaseHealthy(ctx.Req.Context()) {
data.Set("database", "failing") data.Set("database", "failing")
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(503) ctx.Resp.WriteHeader(http.StatusServiceUnavailable)
} else { } else {
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(200) ctx.Resp.WriteHeader(http.StatusOK)
} }
dataBytes, err := data.EncodePretty() dataBytes, err := data.EncodePretty()

View File

@ -55,7 +55,7 @@ func TestAPIEndpoint_Metrics_QueryMetricsV2(t *testing.T) {
QueryDataHandlerFunc: func(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { QueryDataHandlerFunc: func(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
resp := backend.Responses{ resp := backend.Responses{
"A": backend.DataResponse{ "A": backend.DataResponse{
Error: fmt.Errorf("query failed"), Error: errors.New("query failed"),
}, },
} }
return &backend.QueryDataResponse{Responses: resp}, nil return &backend.QueryDataResponse{Responses: resp}, nil
@ -103,7 +103,7 @@ func TestAPIEndpoint_Metrics_PluginDecryptionFailure(t *testing.T) {
QueryDataHandlerFunc: func(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { QueryDataHandlerFunc: func(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
resp := backend.Responses{ resp := backend.Responses{
"A": backend.DataResponse{ "A": backend.DataResponse{
Error: fmt.Errorf("query failed"), Error: errors.New("query failed"),
}, },
} }
return &backend.QueryDataResponse{Responses: resp}, nil return &backend.QueryDataResponse{Responses: resp}, nil

View File

@ -2,7 +2,7 @@ package api
import ( import (
"context" "context"
"fmt" "errors"
"sort" "sort"
"strconv" "strconv"
@ -85,23 +85,21 @@ func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, s
dashboardTagOrder := make(map[string]int) dashboardTagOrder := make(map[string]int)
for i, item := range playlistItems { for i, item := range playlistItems {
if item.Type == "dashboard_by_id" { switch item.Type {
case "dashboard_by_id":
dashboardID, _ := strconv.ParseInt(item.Value, 10, 64) dashboardID, _ := strconv.ParseInt(item.Value, 10, 64)
dashboardByIDs = append(dashboardByIDs, dashboardID) dashboardByIDs = append(dashboardByIDs, dashboardID)
dashboardIDOrder[dashboardID] = i dashboardIDOrder[dashboardID] = i
} case "dashboard_by_tag":
if item.Type == "dashboard_by_tag" {
dashboardByTag = append(dashboardByTag, item.Value) dashboardByTag = append(dashboardByTag, item.Value)
dashboardTagOrder[item.Value] = i dashboardTagOrder[item.Value] = i
} case "dashboard_by_uid":
return nil, errors.New("dashboard_by_uid not supported by this deprecated API")
if item.Type == "dashboard_by_uid" { default:
return nil, fmt.Errorf("dashboard_by_uid not supported by this deprecated API")
} }
} }
var k, _ = hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder) k, _ := hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
result = append(result, k...) result = append(result, k...)
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...) result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)

View File

@ -3,6 +3,7 @@ package api
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -35,7 +36,7 @@ func TestGetPluginDashboards(t *testing.T) {
}, },
}, },
unexpectedErrors: map[string]error{ unexpectedErrors: map[string]error{
"boom": fmt.Errorf("BOOM"), "boom": errors.New("BOOM"),
}, },
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -889,7 +890,7 @@ func TestNewDataSourceProxy_MSSQL(t *testing.T) {
description: "Invalid ODBC URL", description: "Invalid ODBC URL",
url: `localhost\instance::1433`, url: `localhost\instance::1433`,
err: datasource.URLValidationError{ err: datasource.URLValidationError{
Err: fmt.Errorf(`unrecognized MSSQL URL format: "localhost\\instance::1433"`), Err: errors.New(`unrecognized MSSQL URL format: "localhost\\instance::1433"`),
URL: `localhost\instance::1433`, URL: `localhost\instance::1433`,
}, },
}, },