Skip to content

Commit

Permalink
Create a simple Errors struct to replace multierror
Browse files Browse the repository at this point in the history
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
mattmoor committed Dec 17, 2020
1 parent 0c8b1bd commit 701c271
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 14 deletions.
10 changes: 5 additions & 5 deletions estargz/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"strings"
"sync"

"github.com/hashicorp/go-multierror"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -431,19 +430,20 @@ func (tf *tempFiles) TempFile(dir, pattern string) (*os.File, error) {
return f, nil
}

func (tf *tempFiles) CleanupAll() (allErr error) {
func (tf *tempFiles) CleanupAll() error {
tf.filesMu.Lock()
defer tf.filesMu.Unlock()
var allErr *Errors
for _, f := range tf.files {
if err := f.Close(); err != nil {
allErr = multierror.Append(allErr, err)
allErr = allErr.Append(err)
}
if err := os.Remove(f.Name()); err != nil {
allErr = multierror.Append(allErr, err)
allErr = allErr.Append(err)
}
}
tf.files = nil
return nil
return allErr
}

func newCountReader(r io.ReaderAt) (*countReader, error) {
Expand Down
46 changes: 46 additions & 0 deletions estargz/errors.go
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)
48 changes: 48 additions & 0 deletions estargz/errors_test.go
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)
}
}
8 changes: 4 additions & 4 deletions estargz/estargz.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"sync"
"time"

"github.com/hashicorp/go-multierror"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -739,11 +738,13 @@ func footerBytes(tocOff int64) []byte {
}

func parseFooter(p []byte) (tocOffset int64, footerSize int64, rErr error) {
var allErr *Errors

tocOffset, err := parseEStargzFooter(p)
if err == nil {
return tocOffset, FooterSize, nil
}
rErr = multierror.Append(rErr, err)
allErr = allErr.Append(err)

pad := len(p) - legacyFooterSize
if pad < 0 {
Expand All @@ -753,8 +754,7 @@ func parseFooter(p []byte) (tocOffset int64, footerSize int64, rErr error) {
if err == nil {
return tocOffset, legacyFooterSize, nil
}
rErr = multierror.Append(rErr, err)
return
return 0, 0, allErr.Append(err)
}

func parseEStargzFooter(p []byte) (tocOffset int64, err error) {
Expand Down
1 change: 0 additions & 1 deletion estargz/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/containerd/stargz-snapshotter/estargz
go 1.13

require (
github.com/hashicorp/go-multierror v1.1.0
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
Expand Down
4 changes: 0 additions & 4 deletions estargz/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down

0 comments on commit 701c271

Please sign in to comment.