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 {
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 == "" {
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) {
@ -530,19 +530,19 @@ func (hs *HTTPServer) configureHttps() error {
func (hs *HTTPServer) configureHttp2() error {
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 == "" {
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) {
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) {
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{
@ -664,9 +664,8 @@ func (hs *HTTPServer) healthzHandler(ctx *web.Context) {
return
}
ctx.Resp.WriteHeader(200)
_, err := ctx.Resp.Write([]byte("Ok"))
if err != nil {
ctx.Resp.WriteHeader(http.StatusOK)
if _, err := ctx.Resp.Write([]byte("Ok")); err != nil {
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()) {
data.Set("database", "failing")
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(503)
ctx.Resp.WriteHeader(http.StatusServiceUnavailable)
} else {
ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
ctx.Resp.WriteHeader(200)
ctx.Resp.WriteHeader(http.StatusOK)
}
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) {
resp := backend.Responses{
"A": backend.DataResponse{
Error: fmt.Errorf("query failed"),
Error: errors.New("query failed"),
},
}
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) {
resp := backend.Responses{
"A": backend.DataResponse{
Error: fmt.Errorf("query failed"),
Error: errors.New("query failed"),
},
}
return &backend.QueryDataResponse{Responses: resp}, nil

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@ -889,7 +890,7 @@ func TestNewDataSourceProxy_MSSQL(t *testing.T) {
description: "Invalid ODBC URL",
url: `localhost\instance::1433`,
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`,
},
},