From e8ed695f82d5cff26d1672f96db3dd7dce4a2018 Mon Sep 17 00:00:00 2001 From: Mustafa Abdelrahman Date: Tue, 1 Aug 2023 14:25:46 +0200 Subject: [PATCH] Wait for `go1.21` to use `errors.Join` for now use an adhoc wrapper Signed-off-by: Mustafa Abdelrahman --- .../definitions/routegroupvalidator.go | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/dataclients/kubernetes/definitions/routegroupvalidator.go b/dataclients/kubernetes/definitions/routegroupvalidator.go index d1843950e2..e6570be470 100644 --- a/dataclients/kubernetes/definitions/routegroupvalidator.go +++ b/dataclients/kubernetes/definitions/routegroupvalidator.go @@ -1,7 +1,7 @@ package definitions import ( - "errors" + "fmt" "github.com/zalando/skipper/eskip" ) @@ -18,19 +18,32 @@ func ValidateRouteGroup(rg *RouteGroupItem) error { func ValidateRouteGroupList(rgl RouteGroupList) error { var err error for _, rg := range rgl.Items { - err = errors.Join(err, defaultRouteGroupValidator.Validate(rg)) + // TODO: Use errors.Join + err = wrapError(err, defaultRouteGroupValidator.Validate(rg)) } return err } +func wrapError(err1 error, err2 error) error { + if err1 == nil && err2 == nil { + return nil + } else if err1 == nil { + return err2 + } else if err2 == nil { + return err1 + } else { + return fmt.Errorf("%w\n%w", err1, err2) + } +} + func (rgv *RouteGroupValidator) Validate(item *RouteGroupItem) error { err := rgv.basicValidation(item) if err != nil { return err } - err = errors.Join(err, rgv.filtersValidation(item)) - - err = errors.Join(err, rgv.predicatesValidation(item)) + err = wrapError(err, rgv.filtersValidation(item)) + // TODO: Use errors.Join + err = wrapError(err, rgv.predicatesValidation(item)) return err } @@ -59,7 +72,8 @@ func (rgv *RouteGroupValidator) filtersValidation(item *RouteGroupItem) error { for _, r := range item.Spec.Routes { for _, f := range r.Filters { _, e := eskip.ParseFilters(f) - err = errors.Join(err, e) + // TODO: Use errors.Join + err = wrapError(err, e) } } @@ -71,7 +85,8 @@ func (rgv *RouteGroupValidator) predicatesValidation(item *RouteGroupItem) error for _, r := range item.Spec.Routes { for _, p := range r.Predicates { _, e := eskip.ParsePredicates(p) - err = errors.Join(err, e) + // TODO: Use errors.Join + err = wrapError(err, e) } } return err