Skip to content

Commit 3271652

Browse files
authored
Merge pull request #83 from Icinga/allow-matching-config-validation-errors
Allow matching config validation errors
2 parents a65f18b + e47c2d6 commit 3271652

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

config/config.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,23 @@ import (
6060
// must be a non-nil struct pointer.
6161
var ErrInvalidArgument = stderrors.New("invalid argument")
6262

63+
// ErrInvalidConfiguration is attached to errors returned by [FromYAMLFile] or [FromEnv] when
64+
// the configuration is invalid,
65+
// i.e. if the Validate method of the provided [Validator] interface returns an error,
66+
// which is then propagated by these functions.
67+
// Note that for such errors, errors.Is() will recognize both ErrInvalidConfiguration and
68+
// the original errors returned from Validate.
69+
var ErrInvalidConfiguration = stderrors.New("invalid configuration")
70+
6371
// FromYAMLFile parses the given YAML file and stores the result
6472
// in the value pointed to by v. If v is nil or not a struct pointer,
6573
// FromYAMLFile returns an [ErrInvalidArgument] error.
6674
// It is possible to define default values via the struct tag `default`.
6775
// The function also validates the configuration using the Validate method
6876
// of the provided [Validator] interface.
77+
// Any error returned from Validate is propagated with [ErrInvalidConfiguration] attached,
78+
// allowing errors.Is() checks on the returned errors to recognize both ErrInvalidConfiguration and
79+
// the original errors returned from Validate.
6980
//
7081
// Example usage:
7182
//
@@ -114,7 +125,7 @@ func FromYAMLFile(name string, v Validator) error {
114125
}
115126

116127
if err := v.Validate(); err != nil {
117-
return errors.Wrap(err, "invalid configuration")
128+
return fmt.Errorf("%w: %w", ErrInvalidConfiguration, errors.WithStack(err))
118129
}
119130

120131
return nil
@@ -125,6 +136,9 @@ type EnvOptions = env.Options
125136

126137
// FromEnv parses environment variables and stores the result in the value pointed to by v.
127138
// If v is nil or not a struct pointer, FromEnv returns an [ErrInvalidArgument] error.
139+
// Any error returned from Validate is propagated with [ErrInvalidConfiguration] attached,
140+
// allowing errors.Is() checks on the returned errors to recognize both ErrInvalidConfiguration and
141+
// the original errors returned from Validate.
128142
func FromEnv(v Validator, options EnvOptions) error {
129143
if err := validateNonNilStructPointer(v); err != nil {
130144
return errors.WithStack(err)
@@ -139,7 +153,7 @@ func FromEnv(v Validator, options EnvOptions) error {
139153
}
140154

141155
if err := v.Validate(); err != nil {
142-
return errors.Wrap(err, "invalid configuration")
156+
return fmt.Errorf("%w: %w", ErrInvalidConfiguration, errors.WithStack(err))
143157
}
144158

145159
return nil

0 commit comments

Comments
 (0)