Skip to content

Commit

Permalink
Display error when failed to load configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 12, 2019
1 parent fe2ce9a commit 6c4ae88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 9 additions & 4 deletions configor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ func (configor *Configor) GetErrorOnUnmatchedKeys() bool {
}

// Load will unmarshal configurations to struct from files that you provide
func (configor *Configor) Load(config interface{}, files ...string) error {
func (configor *Configor) Load(config interface{}, files ...string) (err error) {
defer func() {
if configor.Config.Debug || configor.Config.Verbose {
if err != nil {
fmt.Printf("Failed to load configuration from %v, got %v\n", files, err)
}

fmt.Printf("Configuration:\n %#v\n", config)
}
}()
Expand All @@ -81,16 +85,17 @@ func (configor *Configor) Load(config interface{}, files ...string) error {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Loading configurations from file '%v'...\n", file)
}
if err := processFile(config, file, configor.GetErrorOnUnmatchedKeys()); err != nil {
if err = processFile(config, file, configor.GetErrorOnUnmatchedKeys()); err != nil {
return err
}
}

prefix := configor.getENVPrefix(config)
if prefix == "-" {
return configor.processTags(config)
err = configor.processTags(config)
}
return configor.processTags(config, prefix)
err = configor.processTags(config, prefix)
return
}

// ENV return environment
Expand Down
3 changes: 1 addition & 2 deletions configor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type testConfig struct {
}

func generateDefaultConfig() testConfig {
config := testConfig{
return testConfig{
APPName: "configor",
Hosts: []string{"http://example.org", "http://jinzhu.me"},
DB: struct {
Expand All @@ -65,7 +65,6 @@ func generateDefaultConfig() testConfig {
Description: "This is an anonymous embedded struct whose environment variables should NOT include 'ANONYMOUS'",
},
}
return config
}

func TestLoadNormaltestConfig(t *testing.T) {
Expand Down

0 comments on commit 6c4ae88

Please sign in to comment.