-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from pohly/error-dependencies
remove usage of github.com/hashicorp/go-multierror
- Loading branch information
Showing
5 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright © 2022 The CDI Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package multierror | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// New combines several errors into a single error. Parameters that are nil are | ||
// ignored. If no errors are passed in or all parameters are nil, then the | ||
// result is also nil. | ||
func New(errors ...error) error { | ||
// Filter out nil entries. | ||
numErrors := 0 | ||
for _, err := range errors { | ||
if err != nil { | ||
errors[numErrors] = err | ||
numErrors++ | ||
} | ||
} | ||
if numErrors == 0 { | ||
return nil | ||
} | ||
return multiError(errors[0:numErrors]) | ||
} | ||
|
||
// multiError is the underlying implementation used by New. | ||
// | ||
// Beware that a null multiError is not the same as a nil error. | ||
type multiError []error | ||
|
||
// multiError returns all individual error strings concatenated with "\n" | ||
func (e multiError) Error() string { | ||
var builder strings.Builder | ||
for i, err := range e { | ||
if i > 0 { | ||
_, _ = builder.WriteString("\n") | ||
} | ||
_, _ = builder.WriteString(err.Error()) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// Append returns a new multi error all errors concatenated. Errors that are | ||
// multi errors get flattened, nil is ignored. | ||
func Append(err error, errors ...error) error { | ||
var result multiError | ||
if m, ok := err.(multiError); ok { | ||
result = m | ||
} else if err != nil { | ||
result = append(result, err) | ||
} | ||
|
||
for _, e := range errors { | ||
if e == nil { | ||
continue | ||
} | ||
if m, ok := e.(multiError); ok { | ||
result = append(result, m...) | ||
} else { | ||
result = append(result, e) | ||
} | ||
} | ||
if len(result) == 0 { | ||
return nil | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright © 2022 The CDI Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package multierror | ||
|
||
import ( | ||
"testing" | ||
"errors" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
assert.Equal(t, nil, New()) | ||
assert.Equal(t, nil, New(nil)) | ||
assert.Equal(t, nil, New(nil, nil)) | ||
assert.Equal(t, "hello\nworld", New(errors.New("hello"), errors.New("world")).Error()) | ||
} | ||
|
||
func TestAppend(t *testing.T) { | ||
assert.Equal(t, nil, Append(nil)) | ||
assert.Equal(t, nil, Append(nil, nil)) | ||
assert.Equal(t, multiError{errors.New("hello"), errors.New("world"), errors.New("x"), errors.New("y")}, | ||
Append(New(errors.New("hello"), errors.New("world")), New(errors.New("x"), nil, errors.New("y"))), nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters