Chore: Remove Wrap (#50048)

* Chore: Remove Wrap and Wrapf

* Fix: Add error check
This commit is contained in:
Kat Yang
2022-06-03 03:24:24 -04:00
committed by GitHub
parent 53cb94a2ad
commit 3c3039f5b3
34 changed files with 104 additions and 117 deletions

View File

@@ -1,6 +1,7 @@
package commands
import (
"fmt"
"strings"
"github.com/fatih/color"
@@ -14,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/urfave/cli/v2"
)
@@ -24,12 +24,12 @@ func runRunnerCommand(command func(commandLine utils.CommandLine, runner runner.
cfg, err := initCfg(cmd)
if err != nil {
return errutil.Wrap("failed to load configuration", err)
return fmt.Errorf("%v: %w", "failed to load configuration", err)
}
r, err := runner.Initialize(cfg)
if err != nil {
return errutil.Wrap("failed to initialize runner", err)
return fmt.Errorf("%v: %w", "failed to initialize runner", err)
}
if err := command(cmd, r); err != nil {
@@ -47,17 +47,17 @@ func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore
cfg, err := initCfg(cmd)
if err != nil {
return errutil.Wrap("failed to load configuration", err)
return fmt.Errorf("%v: %w", "failed to load configuration", err)
}
tracer, err := tracing.ProvideService(cfg)
if err != nil {
return errutil.Wrap("failed to initialize tracer service", err)
return fmt.Errorf("%v: %w", "failed to initialize tracer service", err)
}
sqlStore, err := sqlstore.ProvideService(cfg, nil, &migrations.OSSMigrations{}, tracer)
if err != nil {
return errutil.Wrap("failed to initialize SQL store", err)
return fmt.Errorf("%v: %w", "failed to initialize SQL store", err)
}
if err := command(cmd, sqlStore); err != nil {

View File

@@ -3,6 +3,7 @@ package datamigrations
import (
"context"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
@@ -89,7 +90,7 @@ func updateRows(session *sqlstore.DBSession, rows []map[string][]byte, passwordF
data, err := json.Marshal(newSecureJSONData)
if err != nil {
return 0, errutil.Wrap("marshaling newSecureJsonData failed", err)
return 0, fmt.Errorf("%v: %w", "marshaling newSecureJsonData failed", err)
}
newRow := map[string]interface{}{"secure_json_data": data, passwordFieldName: ""}

View File

@@ -117,7 +117,7 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine, client utils
// Create temp file for downloading zip file
tmpFile, err := ioutil.TempFile("", "*.zip")
if err != nil {
return errutil.Wrap("failed to create temporary file", err)
return fmt.Errorf("%v: %w", "failed to create temporary file", err)
}
defer func() {
if err := os.Remove(tmpFile.Name()); err != nil {
@@ -130,16 +130,16 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine, client utils
if err := tmpFile.Close(); err != nil {
logger.Warn("Failed to close file", "err", err)
}
return errutil.Wrap("failed to download plugin archive", err)
return fmt.Errorf("%v: %w", "failed to download plugin archive", err)
}
err = tmpFile.Close()
if err != nil {
return errutil.Wrap("failed to close tmp file", err)
return fmt.Errorf("%v: %w", "failed to close tmp file", err)
}
err = extractFiles(tmpFile.Name(), pluginName, pluginFolder, isInternal)
if err != nil {
return errutil.Wrap("failed to extract plugin archive", err)
return fmt.Errorf("%v: %w", "failed to extract plugin archive", err)
}
logger.Infof("%s Installed %s successfully \n", color.GreenString("✔"), pluginName)
@@ -275,7 +275,7 @@ func extractFiles(archiveFile string, pluginName string, dstDir string, allowSym
// We can ignore gosec G304 here since it makes sense to give all users read access
// nolint:gosec
if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
return errutil.Wrap("failed to create directory to extract plugin files", err)
return fmt.Errorf("%v: %w", "failed to create directory to extract plugin files", err)
}
if isSymlink(zf) {
@@ -291,7 +291,7 @@ func extractFiles(archiveFile string, pluginName string, dstDir string, allowSym
}
if err := extractFile(zf, dstPath); err != nil {
return errutil.Wrap("failed to extract file", err)
return fmt.Errorf("%v: %w", "failed to extract file", err)
}
}
@@ -306,11 +306,11 @@ func extractSymlink(file *zip.File, filePath string) error {
// symlink target is the contents of the file
src, err := file.Open()
if err != nil {
return errutil.Wrap("failed to extract file", err)
return fmt.Errorf("%v: %w", "failed to extract file", err)
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, src); err != nil {
return errutil.Wrap("failed to copy symlink contents", err)
return fmt.Errorf("%v: %w", "failed to copy symlink contents", err)
}
if err := os.Symlink(strings.TrimSpace(buf.String()), filePath); err != nil {
return errutil.Wrapf(err, "failed to make symbolic link for %v", filePath)
@@ -340,7 +340,7 @@ func extractFile(file *zip.File, filePath string) (err error) {
return fmt.Errorf("file %q is in use - please stop Grafana, install the plugin and restart Grafana", filePath)
}
return errutil.Wrap("failed to open file", err)
return fmt.Errorf("%v: %w", "failed to open file", err)
}
defer func() {
err = dst.Close()
@@ -348,7 +348,7 @@ func extractFile(file *zip.File, filePath string) (err error) {
src, err := file.Open()
if err != nil {
return errutil.Wrap("failed to extract file", err)
return fmt.Errorf("%v: %w", "failed to extract file", err)
}
defer func() {
err = src.Close()

View File

@@ -16,7 +16,6 @@ import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"github.com/grafana/grafana/pkg/util/errutil"
)
type GrafanaComClient struct {
@@ -28,10 +27,10 @@ func (client *GrafanaComClient) GetPlugin(pluginId, repoUrl string) (models.Plug
body, err := sendRequestGetBytes(HttpClient, repoUrl, "repo", pluginId)
if err != nil {
if errors.Is(err, ErrNotFoundError) {
return models.Plugin{}, errutil.Wrap(
return models.Plugin{}, fmt.Errorf("%v: %w",
fmt.Sprintf("Failed to find requested plugin, check if the plugin_id (%s) is correct", pluginId), err)
}
return models.Plugin{}, errutil.Wrap("Failed to send request", err)
return models.Plugin{}, fmt.Errorf("%v: %w", "Failed to send request", err)
}
var data models.Plugin
@@ -52,11 +51,11 @@ func (client *GrafanaComClient) DownloadFile(pluginName string, tmpFile *os.File
// nolint:gosec
f, err := os.Open(url)
if err != nil {
return errutil.Wrap("Failed to read plugin archive", err)
return fmt.Errorf("%v: %w", "Failed to read plugin archive", err)
}
_, err = io.Copy(tmpFile, f)
if err != nil {
return errutil.Wrap("Failed to copy plugin archive", err)
return fmt.Errorf("%v: %w", "Failed to copy plugin archive", err)
}
return nil
}
@@ -93,7 +92,7 @@ func (client *GrafanaComClient) DownloadFile(pluginName string, tmpFile *os.File
// slow network. As this is CLI operation hanging is not a big of an issue as user can just abort.
bodyReader, err := sendRequest(HttpClientNoTimeout, url)
if err != nil {
return errutil.Wrap("Failed to send request", err)
return fmt.Errorf("%v: %w", "Failed to send request", err)
}
defer func() {
if err := bodyReader.Close(); err != nil {
@@ -104,7 +103,7 @@ func (client *GrafanaComClient) DownloadFile(pluginName string, tmpFile *os.File
w := bufio.NewWriter(tmpFile)
h := sha256.New()
if _, err = io.Copy(w, io.TeeReader(bodyReader, h)); err != nil {
return errutil.Wrap("failed to compute SHA256 checksum", err)
return fmt.Errorf("%v: %w", "failed to compute SHA256 checksum", err)
}
if err := w.Flush(); err != nil {
return fmt.Errorf("failed to write to %q: %w", tmpFile.Name(), err)
@@ -120,7 +119,7 @@ func (client *GrafanaComClient) ListAllPlugins(repoUrl string) (models.PluginRep
if err != nil {
logger.Info("Failed to send request", "error", err)
return models.PluginRepo{}, errutil.Wrap("Failed to send request", err)
return models.PluginRepo{}, fmt.Errorf("%v: %w", "Failed to send request", err)
}
var data models.PluginRepo

View File

@@ -1,11 +1,11 @@
package utils
import (
"fmt"
"os"
"path/filepath"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/util/errutil"
)
func GetGrafanaPluginDir(currentOS string) string {
@@ -22,7 +22,7 @@ func GetGrafanaPluginDir(currentOS string) string {
func getGrafanaRoot() (string, error) {
ex, err := os.Executable()
if err != nil {
return "", errutil.Wrap("failed to get executable path", err)
return "", fmt.Errorf("%v: %w", "failed to get executable path", err)
}
exPath := filepath.Dir(ex)
_, last := filepath.Split(exPath)