-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a simple Errors struct to replace multierror
This creates a simple Errors struct that implements error to aggregate multiple errors as a single error. This simplifies the dependency structure of this otherwise nearly leaf package. Fixes: #205
- Loading branch information
Showing
6 changed files
with
103 additions
and
14 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,46 @@ | ||
/* | ||
Copyright The containerd 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 estargz | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Errors provides an abstraction for accumulating multiple errors. | ||
type Errors []error | ||
|
||
// Append adds an error to the Errors | ||
func (e *Errors) Append(err error) *Errors { | ||
if e == nil { | ||
return &Errors{err} | ||
} | ||
errs := append(*e, err) | ||
return &errs | ||
} | ||
|
||
// Error implements error | ||
func (e *Errors) Error() string { | ||
points := make([]string, len(*e)+1) | ||
points[0] = fmt.Sprintf("%d error(s) occurred:", len(*e)) | ||
for i, err := range *e { | ||
points[i+1] = fmt.Sprintf("* %s", err) | ||
} | ||
return strings.Join(points, "\n\t") | ||
} | ||
|
||
var _ error = (*Errors)(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright The containerd 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 estargz | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func TestErrors(t *testing.T) { | ||
var err0 *Errors | ||
|
||
err1 := err0.Append(errors.New("foo")) | ||
err2 := err1.Append(errors.New("bar")) | ||
err3 := err2.Append(errors.New("baz")) | ||
|
||
want := `3 error(s) occurred: | ||
* foo | ||
* bar | ||
* baz` | ||
|
||
if got := err3.Error(); want != got { | ||
t.Errorf("Error() = %v, wanted %v", got, want) | ||
} | ||
|
||
want = `2 error(s) occurred: | ||
* foo | ||
* bar` | ||
|
||
if got := err2.Error(); want != got { | ||
t.Errorf("Error() = %v, wanted %v", got, want) | ||
} | ||
} |
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
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