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 { if ok {
conflictingPath := paths[transformer.MigrationRank] conflictingPath := paths[transformer.MigrationRank]
if conflictingPath != cleanPath { 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 paths[transformer.MigrationRank] = cleanPath

View File

@ -1,7 +1,6 @@
package postgres package postgres
import ( import (
"errors"
"fmt" "fmt"
) )
@ -34,5 +33,5 @@ func ErrUnableToSetNode(setErr error) error {
} }
func formatError(msg, err string) 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 package builder
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -68,7 +67,7 @@ func (b *builder) BuildPlugin() error {
// Build the .go file into a .so plugin // Build the .go file into a .so plugin
execErr := exec.Command("go", "build", "-buildmode=plugin", "-o", soFile, b.goFile).Run() execErr := exec.Command("go", "build", "-buildmode=plugin", "-o", soFile, b.goFile).Run()
if execErr != nil { 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 return nil
} }
@ -94,7 +93,7 @@ func (b *builder) setupBuildEnv() error {
copyErr := helpers.CopyDir(src, dst, "vendor") copyErr := helpers.CopyDir(src, dst, "vendor")
if copyErr != nil { 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) // 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 ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"github.com/lib/pq" "github.com/lib/pq"
"github.com/pressly/goose" "github.com/pressly/goose"
@ -59,7 +58,7 @@ func (m *manager) setDB() error {
} }
dbConnector, err := pq.NewConnector(pgStr) dbConnector, err := pq.NewConnector(pgStr)
if err != nil { 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) m.db = sql.OpenDB(dbConnector)
return nil return nil
@ -99,12 +98,12 @@ func (m *manager) setupMigrationEnv() error {
removeErr := os.RemoveAll(m.tmpMigDir) removeErr := os.RemoveAll(m.tmpMigDir)
if removeErr != nil { if removeErr != nil {
removeErrString := "unable to remove file found at %s where tmp directory needs to be written: %s" 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)) mkdirErr := os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
if mkdirErr != nil { if mkdirErr != nil {
mkdirErrString := "unable to create temporary migration directory %s: %s" 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 return nil
@ -145,18 +144,18 @@ func (m *manager) fixAndRun(path string) error {
if m.db == nil { if m.db == nil {
setErr := m.setDB() setErr := m.setDB()
if setErr != nil { 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 // Fix the migrations
fixErr := goose.Fix(m.tmpMigDir) fixErr := goose.Fix(m.tmpMigDir)
if fixErr != nil { 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 // Run the copied migrations with goose
upErr := goose.Up(m.db, m.tmpMigDir) upErr := goose.Up(m.db, m.tmpMigDir)
if upErr != nil { 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 return nil
} }

View File

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