deps: replace multierr with native errors.Join

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-02-07 12:56:25 +01:00
parent 955316c661
commit 12c866bcb9
32 changed files with 173 additions and 159 deletions

View file

@ -36,7 +36,6 @@ import (
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
"go.uber.org/multierr"
)
// Measurements is a required alias since docgen is not able to work with
@ -586,12 +585,10 @@ func (c *Config) Validate(force bool) error {
return err
}
var validationErrors error
var validationErrMsgs []string
for _, e := range errs {
validationErrors = multierr.Append(
validationErrors,
errors.New(e.Translate(trans)),
)
validationErrMsgs = append(validationErrMsgs, e.Translate(trans))
}
return validationErrors
return &ValidationError{validationErrMsgs: validationErrMsgs}
}

View file

@ -23,7 +23,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"go.uber.org/multierr"
)
func TestMain(m *testing.M) {
@ -185,19 +184,13 @@ func TestNewWithDefaultOptions(t *testing.T) {
}
func TestValidate(t *testing.T) {
const defaultErrCount = 21 // expect this number of error messages by default because user-specific values are not set and multiple providers are defined by default
const azErrCount = 9
const gcpErrCount = 6
testCases := map[string]struct {
cnf *Config
wantErr bool
wantErrCount int
cnf *Config
wantErr bool
}{
"default config is not valid": {
cnf: Default(),
wantErr: true,
wantErrCount: defaultErrCount,
cnf: Default(),
wantErr: true,
},
"v0 is one error": {
cnf: func() *Config {
@ -205,8 +198,7 @@ func TestValidate(t *testing.T) {
cnf.Version = "v0"
return cnf
}(),
wantErr: true,
wantErrCount: defaultErrCount + 1,
wantErr: true,
},
"v0 and negative state disk are two errors": {
cnf: func() *Config {
@ -215,8 +207,7 @@ func TestValidate(t *testing.T) {
cnf.StateDiskSizeGB = -1
return cnf
}(),
wantErr: true,
wantErrCount: defaultErrCount + 2,
wantErr: true,
},
"default Azure config is not valid": {
cnf: func() *Config {
@ -226,8 +217,7 @@ func TestValidate(t *testing.T) {
cnf.Provider.Azure = az
return cnf
}(),
wantErr: true,
wantErrCount: azErrCount,
wantErr: true,
},
"Azure config with all required fields is valid": {
cnf: func() *Config {
@ -255,8 +245,7 @@ func TestValidate(t *testing.T) {
cnf.Provider.GCP = gcp
return cnf
}(),
wantErr: true,
wantErrCount: gcpErrCount,
wantErr: true,
},
"GCP config with all required fields is valid": {
cnf: func() *Config {
@ -302,7 +291,6 @@ func TestValidate(t *testing.T) {
err := tc.cnf.Validate(false)
if tc.wantErr {
assert.Error(err)
assert.Len(multierr.Errors(err), tc.wantErrCount)
return
}
assert.NoError(err)

View file

@ -10,7 +10,6 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"sort"
"strconv"
@ -25,22 +24,27 @@ import (
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"go.uber.org/multierr"
"golang.org/x/mod/semver"
)
// DisplayValidationErrors shows all validation errors inside configError as one formatted string.
func DisplayValidationErrors(errWriter io.Writer, configError error) error {
errs := multierr.Errors(configError)
if errs != nil {
fmt.Fprintln(errWriter, "Problems validating config file:")
for _, err := range errs {
fmt.Fprintln(errWriter, "\t"+err.Error())
}
fmt.Fprintln(errWriter, "Fix the invalid entries or generate a new configuration using `constellation config generate`")
return errors.New("invalid configuration")
// ValidationError occurs when the validation of a config fails.
// It contains a list of errors that occurred during validation.
type ValidationError struct {
validationErrMsgs []string
}
func (e *ValidationError) Error() string {
return "invalid configuration"
}
// LongMessage prints the errors that occurred during validation in a verbose and user friendly way.
func (e *ValidationError) LongMessage() string {
msg := "Problems validating config file:\n"
for _, ve := range e.validationErrMsgs {
msg += fmt.Sprintf("\t%s\n", ve)
}
return nil
msg += "Fix the invalid entries or generate a new configuration using `constellation config generate`"
return msg
}
func registerInvalidK8sVersionError(ut ut.Translator) error {