Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow matching config validation errors #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ import (
// must be a non-nil struct pointer.
var ErrInvalidArgument = stderrors.New("invalid argument")

// ErrInvalidConfiguration is attached to errors returned by [FromYAMLFile] or [FromEnv] when
// the configuration is invalid,
// i.e. if the Validate method of the provided [Validator] interface returns an error,
// which is then propagated by these functions.
// Note that for such errors, errors.Is() will recognize both ErrInvalidConfiguration and
// the original errors returned from Validate.
var ErrInvalidConfiguration = stderrors.New("invalid configuration")

// FromYAMLFile parses the given YAML file and stores the result
// in the value pointed to by v. If v is nil or not a struct pointer,
// FromYAMLFile returns an [ErrInvalidArgument] error.
// It is possible to define default values via the struct tag `default`.
// The function also validates the configuration using the Validate method
// of the provided [Validator] interface.
// Any error returned from Validate is propagated with [ErrInvalidConfiguration] attached,
// allowing errors.Is() checks on the returned errors to recognize both ErrInvalidConfiguration and
// the original errors returned from Validate.
//
// Example usage:
//
Expand Down Expand Up @@ -114,7 +125,7 @@ func FromYAMLFile(name string, v Validator) error {
}

if err := v.Validate(); err != nil {
return errors.Wrap(err, "invalid configuration")
return fmt.Errorf("%w: %w", ErrInvalidConfiguration, errors.WithStack(err))
}

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

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

if err := v.Validate(); err != nil {
return errors.Wrap(err, "invalid configuration")
return fmt.Errorf("%w: %w", ErrInvalidConfiguration, errors.WithStack(err))
}

return nil
Expand Down
Loading