Skip to content

Commit

Permalink
Refactor error variable names and update associated tests in metricsf…
Browse files Browse the repository at this point in the history
…orwarder config
  • Loading branch information
bonzofenix committed Aug 9, 2024
1 parent 8e3ac4a commit ab43a97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
19 changes: 9 additions & 10 deletions src/autoscaler/metricsforwarder/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
)

// There are 3 type of errors that this package can return:
// - ReadYamlError
// - ReadFileError
// - ReadEnvironmentError
// - ErrReadYaml
// - ErrReadEnvironment
// - ErrReadVCAPEnvironment

var ReadYamlError = errors.New("failed to read config file")
var ReadFileError = errors.New("failed to open config file")
var ReadEnvironmentError = errors.New("failed to read environment variables")
var ReadVCAPEnvironmentError = errors.New("failed to read VCAP environment variables")
var ErrReadYaml = errors.New("failed to read config file")
var ErrReadEnvironment = errors.New("failed to read environment variables")
var ErrReadVCAPEnvironment = errors.New("failed to read VCAP environment variables")

const (
DefaultMetronAddress = "127.0.0.1:3458"
Expand Down Expand Up @@ -116,20 +115,20 @@ func LoadConfig(filepath string) (*Config, error) {
err = dec.Decode(&conf)

if err != nil {
return nil, fmt.Errorf("%w: %w", ReadYamlError, err)
return nil, fmt.Errorf("%w: %w", ErrReadYaml, err)
}

defer r.Close()
}

err = envconfig.Process("", &conf)
if err != nil {
return nil, fmt.Errorf("%w: %w", ReadEnvironmentError, err)
return nil, fmt.Errorf("%w: %w", ErrReadEnvironment, err)
}

err = loadVCAPEnvs(&conf)
if err != nil {
return nil, fmt.Errorf("%w: %w", ReadVCAPEnvironmentError, err)
return nil, fmt.Errorf("%w: %w", ErrReadVCAPEnvironment, err)
}

return &conf, nil
Expand Down
16 changes: 8 additions & 8 deletions src/autoscaler/metricsforwarder/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ cred_helper_impl: default
})

It("return invalid port error", func() {
Expect(err).To(MatchError(config.ReadEnvironmentError))
Expect(err).To(MatchError(config.ErrReadEnvironment))
Expect(err).To(MatchError(MatchRegexp("converting 'NAN' to type int")))
})

Expand Down Expand Up @@ -206,7 +206,7 @@ server:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal.*into int")))
})
})
Expand All @@ -220,7 +220,7 @@ health:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal.*into int")))
})
})
Expand All @@ -246,7 +246,7 @@ health:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal.*into int")))
})
})
Expand All @@ -272,7 +272,7 @@ health:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal.*into int")))
})
})
Expand All @@ -298,7 +298,7 @@ health:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal .* into time.Duration")))
})
})
Expand Down Expand Up @@ -327,7 +327,7 @@ rate_limit:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal .* into int")))
})
})
Expand Down Expand Up @@ -356,7 +356,7 @@ rate_limit:
})

It("should error", func() {
Expect(err).To(MatchError(config.ReadYamlError))
Expect(err).To(MatchError(config.ErrReadYaml))
Expect(err).To(MatchError(MatchRegexp("cannot unmarshal .* into time.Duration")))
})
})
Expand Down

0 comments on commit ab43a97

Please sign in to comment.