Skip to content

Commit

Permalink
specerror: Add FilterError helper
Browse files Browse the repository at this point in the history
This doesn't save much time for folks who are iterating over the
removed errors (e.g. to print warnings about them), but Aleksa asked
for it [1].

I haven't used it in runtimetest.  I'm waiting for the in-flight
7aa3987 (cmd/runtimetest/main: Use TAP diagnostics for errors,
2017-07-28, opencontainers#439) to settle, since that commit cleans up some of the
runtimetest error-processing code with:

  validations := defaultValidations
  if platform == "linux" {
    validations = append(validations, linuxValidations...)
  }

and a single loop over the constructed validations array.

I initially had FilterError returning (filtered, removed), but golint
didn't like that:

  error should be the last type when returning multiple items

[1]: opencontainers#492 (comment)

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Oct 3, 2017
1 parent a9dbd7e commit be12cce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 4 additions & 12 deletions cmd/oci-runtime-tool/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"

"github.com/hashicorp/go-multierror"
rfc2119 "github.com/opencontainers/runtime-tools/error"
"github.com/opencontainers/runtime-tools/specerror"
"github.com/opencontainers/runtime-tools/validate"
Expand Down Expand Up @@ -37,20 +36,13 @@ var bundleValidateCommand = cli.Command{
}

if err := v.CheckAll(); err != nil {
merr, ok := err.(*multierror.Error)
if !ok {
return err
}
var validationErrors error
for _, err = range merr.Errors {
e, ok := err.(*specerror.Error)
if ok && e.Err.Level < complianceLevel {
err, removed := specerror.FilterError(err, complianceLevel)
if removed != nil {
for _, e := range removed.Errors {
logrus.Warn(e)
continue
}
validationErrors = multierror.Append(validationErrors, err)
}
return validationErrors
return err
}
fmt.Println("Bundle validation succeeded.")
return nil
Expand Down
19 changes: 19 additions & 0 deletions specerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ func FindError(err error, code Code) Code {
}
return NonRFCError
}

// FilterError removes RFC 2119 errors with a level less than 'level'
// from the source error. If the source error is not a multierror, it
// is returned unchanged.
func FilterError(err error, level rfc2119.Level) (filtered error, removed *multierror.Error) {
merr, ok := err.(*multierror.Error)
if !ok {
return err, nil
}
for _, err = range merr.Errors {
e, ok := err.(*Error)
if ok && e.Err.Level < level {
removed = multierror.Append(removed, e)
continue
}
filtered = multierror.Append(filtered, err)
}
return filtered, removed
}

0 comments on commit be12cce

Please sign in to comment.