From 17b17fc8bb690d1f6344e5af1c62b3b37166bc48 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Sun, 15 Jan 2017 21:50:38 +0000 Subject: Add more context to errors --- server/auth/github/github.go | 2 +- server/auth/github/github_test.go | 8 +++----- server/auth/google/google.go | 2 +- server/auth/google/google_test.go | 7 +++---- server/config/config.go | 22 +++++++++++----------- 5 files changed, 19 insertions(+), 22 deletions(-) (limited to 'server') diff --git a/server/auth/github/github.go b/server/auth/github/github.go index 24a4bbf..7628526 100644 --- a/server/auth/github/github.go +++ b/server/auth/github/github.go @@ -32,7 +32,7 @@ func New(c *config.Auth) (auth.Provider, error) { uw[u] = true } if c.ProviderOpts["organization"] == "" && len(uw) == 0 { - return nil, errors.New("github_opts organization and the users whitelist must not be both empty") + return nil, errors.New("either GitHub organization or users whitelist must be specified") } return &Config{ config: &oauth2.Config{ diff --git a/server/auth/github/github_test.go b/server/auth/github/github_test.go index c0b26a4..b0c97d2 100644 --- a/server/auth/github/github_test.go +++ b/server/auth/github/github_test.go @@ -29,11 +29,9 @@ func TestNew(t *testing.T) { func TestNewEmptyOrganization(t *testing.T) { organization = "" - a := assert.New(t) - - _, err := newGithub() - a.EqualError(err, "github_opts organization and the users whitelist must not be both empty") - + if _, err := newGithub(); err == nil { + t.Error("creating a provider without an organization set should return an error") + } organization = "exampleorg" } diff --git a/server/auth/google/google.go b/server/auth/google/google.go index 08a4083..643ecfe 100644 --- a/server/auth/google/google.go +++ b/server/auth/google/google.go @@ -34,7 +34,7 @@ func New(c *config.Auth) (auth.Provider, error) { uw[u] = true } if c.ProviderOpts["domain"] == "" && len(uw) == 0 { - return nil, errors.New("google_opts domain and the users whitelist must not be both empty") + return nil, errors.New("either Google Apps domain or users whitelist must be specified") } return &Config{ diff --git a/server/auth/google/google_test.go b/server/auth/google/google_test.go index b80c4bf..781cf6f 100644 --- a/server/auth/google/google_test.go +++ b/server/auth/google/google_test.go @@ -28,12 +28,11 @@ func TestNew(t *testing.T) { } func TestNewWithoutDomain(t *testing.T) { - a := assert.New(t) - domain = "" - _, err := newGoogle() - a.EqualError(err, "google_opts domain and the users whitelist must not be both empty") + if _, err := newGoogle(); err == nil { + t.Error("creating a provider without a domain set should return an error") + } domain = "example.com" } diff --git a/server/config/config.go b/server/config/config.go index 5f3f458..f2598a0 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -1,7 +1,6 @@ package config import ( - "errors" "fmt" "io" "log" @@ -12,6 +11,7 @@ import ( "github.com/hashicorp/go-multierror" "github.com/mitchellh/mapstructure" "github.com/nsheridan/cashier/server/helpers/vault" + "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -156,14 +156,14 @@ func setFromVault(c *Config) error { } v, err := vault.NewClient(c.Vault.Address, c.Vault.Token) if err != nil { - return err + return errors.Wrap(err, "vault error") } - var errors error + var errs error get := func(value string) string { if strings.HasPrefix(value, "/vault/") { s, err := v.Read(value) if err != nil { - errors = multierror.Append(errors, err) + errs = multierror.Append(errs, err) } return s } @@ -180,12 +180,12 @@ func setFromVault(c *Config) error { c.AWS.AccessKey = get(c.AWS.AccessKey) c.AWS.SecretKey = get(c.AWS.SecretKey) } - return errors + return errors.Wrap(errs, "errors reading from vault") } // Unmarshal the config into a *Config func decode() (*Config, error) { - var errors error + var errs error config := &Config{} configPieces := map[string]interface{}{ "auth": &config.Auth, @@ -200,21 +200,21 @@ func decode() (*Config, error) { continue } if err := mapstructure.WeakDecode(conf[0], val); err != nil { - errors = multierror.Append(errors, err) + errs = multierror.Append(errs, err) } } - return config, errors + return config, errs } // ReadConfig parses a hcl configuration file into a Config struct. func ReadConfig(r io.Reader) (*Config, error) { viper.SetConfigType("hcl") if err := viper.ReadConfig(r); err != nil { - return nil, err + return nil, errors.Wrap(err, "unable to read config") } config, err := decode() if err != nil { - return nil, err + return nil, errors.Wrap(err, "unable to parse config") } if err := setFromVault(config); err != nil { return nil, err @@ -222,7 +222,7 @@ func ReadConfig(r io.Reader) (*Config, error) { setFromEnvironment(config) convertDatastoreConfig(config) if err := verifyConfig(config); err != nil { - return nil, err + return nil, errors.Wrap(err, "unable to verify config") } return config, nil } -- cgit v1.2.3