From a13aa469b4042b24d828067e0bce5f4e8598564a Mon Sep 17 00:00:00 2001 From: Ryan King Date: Thu, 28 Jun 2018 10:25:09 -0700 Subject: [PATCH] user errors.Wrap to give context to errors --- Gopkg.lock | 2 +- Gopkg.toml | 4 ++++ apply/apply.go | 31 ++++++++++++++++--------------- config/config.go | 19 ++++++++++--------- init/init.go | 9 +++++---- util/version.go | 14 ++++++++------ 6 files changed, 44 insertions(+), 35 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index f8e8ada22..e169c38e9 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -141,6 +141,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "0914236150f9fb0b08221a1207d4fe1c7660b694e00213ca6daf03a3aa63eb77" + inputs-digest = "b5f489f0675fc11b14b36689c3fff5d735d145338daad214f36794f81fb9f0ee" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index ea8ec0e8a..afbe4bad4 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -25,3 +25,7 @@ [prune] go-tests = true unused-packages = true + +[[constraint]] + name = "github.com/pkg/errors" + version = "0.8.0" diff --git a/apply/apply.go b/apply/apply.go index b00016888..833e95c7f 100644 --- a/apply/apply.go +++ b/apply/apply.go @@ -12,6 +12,7 @@ import ( "github.com/chanzuckerberg/fogg/templates" "github.com/chanzuckerberg/fogg/util" "github.com/gobuffalo/packr" + "github.com/pkg/errors" "github.com/spf13/afero" ) @@ -20,22 +21,22 @@ const rootPath = "terraform" func Apply(fs afero.Fs, configFile string, tmp *templates.T) error { p, err := plan.Eval(fs, configFile) if err != nil { - return err + return errors.Wrap(err, "unable to evaluate plan") } e := applyRepo(fs, p, &tmp.Repo) if e != nil { - return e + return errors.Wrap(e, "unable to apply repo") } e = applyAccounts(fs, p, &tmp.Account) if e != nil { - return e + return errors.Wrap(e, "unable to apply accounts") } e = applyEnvs(fs, p, &tmp.Env, &tmp.Component) if e != nil { - return e + return errors.Wrap(e, "unable to apply envs") } e = applyGlobal(fs, p.Global, &tmp.Global) @@ -66,11 +67,11 @@ func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) { path := fmt.Sprintf("%s/accounts/%s", rootPath, account) e = fs.MkdirAll(path, 0755) if e != nil { - return e + return errors.Wrap(e, "unable to make directories for accounts") } e = applyTree(accountBox, afero.NewBasePathFs(fs, path), accountPlan) if e != nil { - return e + return errors.Wrap(e, "unable to apply templates to account") } } return nil @@ -81,21 +82,21 @@ func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr path := fmt.Sprintf("%s/envs/%s", rootPath, env) e = fs.MkdirAll(path, 0755) if e != nil { - return e + return errors.Wrap(e, "unable to make directies for envs") } e := applyTree(envBox, afero.NewBasePathFs(fs, path), envPlan) if e != nil { - return e + return errors.Wrap(e, "unable to apply templates to env") } for component, componentPlan := range envPlan.Components { path := fmt.Sprintf("%s/envs/%s/%s", rootPath, env, component) e = fs.MkdirAll(path, 0755) if e != nil { - return e + return errors.Wrap(e, "unable to make directories for component") } e := applyTree(componentBox, afero.NewBasePathFs(fs, path), componentPlan) if e != nil { - return e + return errors.Wrap(e, "unable to apply templates for component") } } } @@ -110,7 +111,7 @@ func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) { err := applyTemplate(sourceFile, dest, basename, subst) if err != nil { - return err + return errors.Wrap(err, "unable to apply template") } // if dest.endswith('.tf'): @@ -129,7 +130,7 @@ func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) { log.Printf("copying %s", path) e = afero.WriteReader(dest, path, sourceFile) if e != nil { - return e + return errors.Wrap(e, "unable to copy file") } } return nil @@ -143,7 +144,7 @@ func touchFile(dest afero.Fs, path string) error { log.Printf("touching %s", path) _, err = dest.Create(path) if err != nil { - return err + return errors.Wrap(err, "unable to touch file") } } else { log.Printf("skipping touch on existing file %s", path) @@ -157,7 +158,7 @@ func createFile(dest afero.Fs, path string, sourceFile io.Reader) error { log.Printf("creating %s", path) err = afero.WriteReader(dest, path, sourceFile) if err != nil { - return err + return errors.Wrap(err, "unable to create file") } } else { log.Printf("skipping create on existing file %s", path) @@ -173,7 +174,7 @@ func applyTemplate(sourceFile io.Reader, dest afero.Fs, path string, overrides i log.Printf("templating %s", path) writer, err := dest.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) if err != nil { - return err + return errors.Wrap(err, "unable to open file") } t := util.OpenTemplate(sourceFile) return t.Execute(writer, overrides) diff --git a/config/config.go b/config/config.go index 9284f9495..b48d985b7 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" + "github.com/pkg/errors" "github.com/spf13/afero" ) @@ -110,21 +111,21 @@ func InitConfig(project, region, bucket, awsProfile, owner string) *Config { func ReadConfig(f io.Reader) (*Config, error) { c := &Config{} - b, err := ioutil.ReadAll(f) - if err != nil { - return nil, err + b, e := ioutil.ReadAll(f) + if e != nil { + return nil, errors.Wrap(e, "unable to read config") } - err2 := json.Unmarshal(b, c) - if err2 != nil { - return nil, err2 + e = json.Unmarshal(b, c) + if e != nil { + return nil, errors.Wrap(e, "unable to parse json config file") } return c, nil } func FindAndReadConfig(fs afero.Fs, configFile string) (*Config, error) { - f, err := fs.Open(configFile) - if err != nil { - return nil, err + f, e := fs.Open(configFile) + if e != nil { + return nil, errors.Wrap(e, "unable to open config file") } reader := io.ReadCloser(f) defer reader.Close() diff --git a/init/init.go b/init/init.go index 346f0bfc7..c0da55cac 100644 --- a/init/init.go +++ b/init/init.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/chanzuckerberg/fogg/config" + "github.com/pkg/errors" prompt "github.com/segmentio/go-prompt" "github.com/spf13/afero" ) @@ -21,11 +22,11 @@ func userPrompt() (string, string, string, string, string) { func writeConfig(fs afero.Fs, config *config.Config) error { json, e := json.MarshalIndent(config, "", " ") if e != nil { - return e + return errors.Wrap(e, "unable to marshal json") } - configFile, e2 := fs.Create("fogg.json") - if e2 != nil { - return e2 + configFile, e := fs.Create("fogg.json") + if e != nil { + return errors.Wrap(e, "unable to create config file fogg.json") } _, e3 := configFile.Write(json) return e3 diff --git a/util/version.go b/util/version.go index 3d0b05718..c284f62bb 100644 --- a/util/version.go +++ b/util/version.go @@ -3,23 +3,25 @@ package util import ( "fmt" "strconv" + + "github.com/pkg/errors" ) var ( - Version = "undefined" - GitSha = "undefined" - Release = "false" - Dirty = "true" + Version = "undefined" + GitSha = "undefined" + Release = "false" + Dirty = "true" ) func VersionString() (string, error) { release, e := strconv.ParseBool(Release) if e != nil { - return "", e + return "", errors.Wrapf(e, "unable to parse version release field %s", Release) } dirty, e := strconv.ParseBool(Dirty) if e != nil { - return "", e + return "", errors.Wrapf(e, "unable to parse version dirty field %s", Dirty) } return versionString(Version, GitSha, release, dirty), nil }