Prefer fmt.Errorf(s) to errors.New(fmt.Sprintf(s))

This commit is contained in:
Rob Mulholand 2019-10-18 10:20:53 -05:00
parent eb2f3c2209
commit 0ded7bcd62
5 changed files with 12 additions and 16 deletions

View File

@ -71,7 +71,7 @@ func (pluginConfig *Plugin) GetMigrationsPaths() ([]string, error) {
if ok {
conflictingPath := paths[transformer.MigrationRank]
if conflictingPath != cleanPath {
return nil, errors.New(fmt.Sprintf("transformer %s has the same migration rank (%d) as another transformer", name, transformer.MigrationRank))
return nil, fmt.Errorf("transformer %s has the same migration rank (%d) as another transformer", name, transformer.MigrationRank)
}
}
paths[transformer.MigrationRank] = cleanPath

View File

@ -1,7 +1,6 @@
package postgres
import (
"errors"
"fmt"
)
@ -34,5 +33,5 @@ func ErrUnableToSetNode(setErr error) error {
}
func formatError(msg, err string) error {
return errors.New(fmt.Sprintf("%s: %s", msg, err))
return fmt.Errorf("%s: %s", msg, err)
}

View File

@ -17,7 +17,6 @@
package builder
import (
"errors"
"fmt"
"os"
"os/exec"
@ -68,7 +67,7 @@ func (b *builder) BuildPlugin() error {
// Build the .go file into a .so plugin
execErr := exec.Command("go", "build", "-buildmode=plugin", "-o", soFile, b.goFile).Run()
if execErr != nil {
return errors.New(fmt.Sprintf("unable to build .so file: %s", execErr.Error()))
return fmt.Errorf("unable to build .so file: %s", execErr.Error())
}
return nil
}
@ -94,7 +93,7 @@ func (b *builder) setupBuildEnv() error {
copyErr := helpers.CopyDir(src, dst, "vendor")
if copyErr != nil {
return errors.New(fmt.Sprintf("unable to copy transformer dependency from %s to %s: %v", src, dst, copyErr))
return fmt.Errorf("unable to copy transformer dependency from %s to %s: %v", src, dst, copyErr)
}
// Have to clear out the copied over vendor lib or plugin won't build (see issue above)

View File

@ -18,7 +18,6 @@ package manager
import (
"database/sql"
"errors"
"fmt"
"github.com/lib/pq"
"github.com/pressly/goose"
@ -59,7 +58,7 @@ func (m *manager) setDB() error {
}
dbConnector, err := pq.NewConnector(pgStr)
if err != nil {
return errors.New(fmt.Sprintf("can't connect to db: %s", err.Error()))
return fmt.Errorf("can't connect to db: %s", err.Error())
}
m.db = sql.OpenDB(dbConnector)
return nil
@ -99,12 +98,12 @@ func (m *manager) setupMigrationEnv() error {
removeErr := os.RemoveAll(m.tmpMigDir)
if removeErr != nil {
removeErrString := "unable to remove file found at %s where tmp directory needs to be written: %s"
return errors.New(fmt.Sprintf(removeErrString, m.tmpMigDir, removeErr.Error()))
return fmt.Errorf(removeErrString, m.tmpMigDir, removeErr.Error())
}
mkdirErr := os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
if mkdirErr != nil {
mkdirErrString := "unable to create temporary migration directory %s: %s"
return errors.New(fmt.Sprintf(mkdirErrString, m.tmpMigDir, mkdirErr.Error()))
return fmt.Errorf(mkdirErrString, m.tmpMigDir, mkdirErr.Error())
}
return nil
@ -145,18 +144,18 @@ func (m *manager) fixAndRun(path string) error {
if m.db == nil {
setErr := m.setDB()
if setErr != nil {
return errors.New(fmt.Sprintf("could not open db: %s", setErr.Error()))
return fmt.Errorf("could not open db: %s", setErr.Error())
}
}
// Fix the migrations
fixErr := goose.Fix(m.tmpMigDir)
if fixErr != nil {
return errors.New(fmt.Sprintf("version fixing for plugin migrations at %s failed: %s", path, fixErr.Error()))
return fmt.Errorf("version fixing for plugin migrations at %s failed: %s", path, fixErr.Error())
}
// Run the copied migrations with goose
upErr := goose.Up(m.db, m.tmpMigDir)
if upErr != nil {
return errors.New(fmt.Sprintf("db migrations for plugin transformers at %s failed: %s", path, upErr.Error()))
return fmt.Errorf("db migrations for plugin transformers at %s failed: %s", path, upErr.Error())
}
return nil
}

View File

@ -17,7 +17,6 @@
package writer
import (
"errors"
"fmt"
. "github.com/dave/jennifer/jen"
@ -89,7 +88,7 @@ func (w *writer) WritePlugin() error {
// Write code to destination file
err = f.Save(goFile)
if err != nil {
return errors.New(fmt.Sprintf("failed to save generated .go file: %s\r\n%s", goFile, err.Error()))
return fmt.Errorf("failed to save generated .go file: %s\r\n%s", goFile, err.Error())
}
return nil
}
@ -107,7 +106,7 @@ func (w *writer) collectTransformers() (map[config.TransformerType][]Code, error
case config.EthContract:
code[config.EthContract] = append(code[config.EthContract], Qual(path, "ContractTransformerInitializer"))
default:
return nil, errors.New(fmt.Sprintf("invalid transformer type %s", transformer.Type))
return nil, fmt.Errorf("invalid transformer type %s", transformer.Type)
}
}