Skip to content

Commit d4004b3

Browse files
authored
Merge pull request #89 from pohly/error-dependencies
remove usage of github.com/hashicorp/go-multierror
2 parents cbac83c + ec52f5a commit d4004b3

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.17
44

55
require (
66
github.com/fsnotify/fsnotify v1.5.1
7-
github.com/hashicorp/go-multierror v1.1.1
87
github.com/opencontainers/runc v1.1.2
98
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
109
github.com/opencontainers/runtime-tools v0.0.0-20190417131837-cd1349b7c47e
@@ -21,6 +20,7 @@ require (
2120
github.com/blang/semver v3.5.1+incompatible // indirect
2221
github.com/davecgh/go-spew v1.1.1 // indirect
2322
github.com/hashicorp/errwrap v1.0.0 // indirect
23+
github.com/hashicorp/go-multierror v1.1.1 // indirect
2424
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2525
github.com/opencontainers/selinux v1.10.0 // indirect
2626
github.com/pmezard/go-difflib v1.0.0 // indirect

internal/multierror/multierror.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright © 2022 The CDI Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package multierror
18+
19+
import (
20+
"strings"
21+
)
22+
23+
// New combines several errors into a single error. Parameters that are nil are
24+
// ignored. If no errors are passed in or all parameters are nil, then the
25+
// result is also nil.
26+
func New(errors ...error) error {
27+
// Filter out nil entries.
28+
numErrors := 0
29+
for _, err := range errors {
30+
if err != nil {
31+
errors[numErrors] = err
32+
numErrors++
33+
}
34+
}
35+
if numErrors == 0 {
36+
return nil
37+
}
38+
return multiError(errors[0:numErrors])
39+
}
40+
41+
// multiError is the underlying implementation used by New.
42+
//
43+
// Beware that a null multiError is not the same as a nil error.
44+
type multiError []error
45+
46+
// multiError returns all individual error strings concatenated with "\n"
47+
func (e multiError) Error() string {
48+
var builder strings.Builder
49+
for i, err := range e {
50+
if i > 0 {
51+
_, _ = builder.WriteString("\n")
52+
}
53+
_, _ = builder.WriteString(err.Error())
54+
}
55+
return builder.String()
56+
}
57+
58+
// Append returns a new multi error all errors concatenated. Errors that are
59+
// multi errors get flattened, nil is ignored.
60+
func Append(err error, errors ...error) error {
61+
var result multiError
62+
if m, ok := err.(multiError); ok {
63+
result = m
64+
} else if err != nil {
65+
result = append(result, err)
66+
}
67+
68+
for _, e := range errors {
69+
if e == nil {
70+
continue
71+
}
72+
if m, ok := e.(multiError); ok {
73+
result = append(result, m...)
74+
} else {
75+
result = append(result, e)
76+
}
77+
}
78+
if len(result) == 0 {
79+
return nil
80+
}
81+
return result
82+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright © 2022 The CDI Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package multierror
18+
19+
import (
20+
"testing"
21+
"errors"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestNew(t *testing.T) {
27+
assert.Equal(t, nil, New())
28+
assert.Equal(t, nil, New(nil))
29+
assert.Equal(t, nil, New(nil, nil))
30+
assert.Equal(t, "hello\nworld", New(errors.New("hello"), errors.New("world")).Error())
31+
}
32+
33+
func TestAppend(t *testing.T) {
34+
assert.Equal(t, nil, Append(nil))
35+
assert.Equal(t, nil, Append(nil, nil))
36+
assert.Equal(t, multiError{errors.New("hello"), errors.New("world"), errors.New("x"), errors.New("y")},
37+
Append(New(errors.New("hello"), errors.New("world")), New(errors.New("x"), nil, errors.New("y"))), nil)
38+
}

pkg/cdi/cache.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import (
2626

2727
stderr "errors"
2828

29+
"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
2930
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3031
"github.com/fsnotify/fsnotify"
31-
"github.com/hashicorp/go-multierror"
3232
oci "github.com/opencontainers/runtime-spec/specs-go"
3333
"github.com/pkg/errors"
3434
)
@@ -127,8 +127,8 @@ func (c *Cache) Refresh() error {
127127

128128
// collect and return cached errors, much like refresh() does it
129129
var result error
130-
for _, err := range c.errors {
131-
result = multierror.Append(result, err...)
130+
for _, errors := range c.errors {
131+
result = multierror.Append(result, errors...)
132132
}
133133
return result
134134
}
@@ -198,11 +198,7 @@ func (c *Cache) refresh() error {
198198
c.devices = devices
199199
c.errors = specErrors
200200

201-
if len(result) > 0 {
202-
return multierror.Append(nil, result...)
203-
}
204-
205-
return nil
201+
return multierror.New(result...)
206202
}
207203

208204
// RefreshIfRequired triggers a refresh if necessary.

schema/schema.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
"net/http"
2626
"path/filepath"
2727
"strings"
28+
"fmt"
2829

2930
"sigs.k8s.io/yaml"
3031

31-
"github.com/hashicorp/go-multierror"
32+
"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
3233
"github.com/pkg/errors"
3334
schema "github.com/xeipuuv/gojsonschema"
3435
)
@@ -227,17 +228,17 @@ func (s *Schema) validate(doc schema.JSONLoader) error {
227228
return &Error{Result: docErr}
228229
}
229230

230-
// Error returns the given Result's error as a multierror(.Error()).
231+
// Error returns the given Result's errors as a single error string.
231232
func (e *Error) Error() string {
232233
if e == nil || e.Result == nil || e.Result.Valid() {
233234
return ""
234235
}
235236

236237
var multi error
237238
for _, err := range e.Result.Errors() {
238-
multi = multierror.Append(multi, errors.Errorf("%v", err))
239+
multi = multierror.Append(multi, fmt.Errorf("%v", err))
239240
}
240-
return strings.TrimRight(multi.Error(), "\n")
241+
return multi.Error()
241242
}
242243

243244
var (

0 commit comments

Comments
 (0)