Skip to content

Commit 91aa0e9

Browse files
committed
specerror: Add FilterError helper
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. [1]: opencontainers#492 (comment) Signed-off-by: W. Trevor King <[email protected]>
1 parent a9dbd7e commit 91aa0e9

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

cmd/oci-runtime-tool/validate.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/hashicorp/go-multierror"
76
rfc2119 "github.com/opencontainers/runtime-tools/error"
87
"github.com/opencontainers/runtime-tools/specerror"
98
"github.com/opencontainers/runtime-tools/validate"
@@ -37,20 +36,13 @@ var bundleValidateCommand = cli.Command{
3736
}
3837

3938
if err := v.CheckAll(); err != nil {
40-
merr, ok := err.(*multierror.Error)
41-
if !ok {
42-
return err
43-
}
44-
var validationErrors error
45-
for _, err = range merr.Errors {
46-
e, ok := err.(*specerror.Error)
47-
if ok && e.Err.Level < complianceLevel {
39+
err, removed := specerror.FilterError(err, complianceLevel)
40+
if removed != nil {
41+
for _, e := range removed.Errors {
4842
logrus.Warn(e)
49-
continue
5043
}
51-
validationErrors = multierror.Append(validationErrors, err)
5244
}
53-
return validationErrors
45+
return err
5446
}
5547
fmt.Println("Bundle validation succeeded.")
5648
return nil

specerror/error.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,22 @@ func FindError(err error, code Code) Code {
168168
}
169169
return NonRFCError
170170
}
171+
172+
// FilterError removes RFC 2119 errors with a level less than 'level'
173+
// from the source error. If the source error is not a multierror, it
174+
// is returned unchanged.
175+
func FilterError(err error, level rfc2119.Level) (filtered error, removed *multierror.Error) {
176+
merr, ok := err.(*multierror.Error)
177+
if !ok {
178+
return err, nil
179+
}
180+
for _, err = range merr.Errors {
181+
e, ok := err.(*Error)
182+
if ok && e.Err.Level < level {
183+
removed = multierror.Append(removed, e)
184+
continue
185+
}
186+
filtered = multierror.Append(filtered, err)
187+
}
188+
return filtered, removed
189+
}

0 commit comments

Comments
 (0)