Skip to content

Commit

Permalink
refactor: Use errors.Join instead of `github.com/hashicorp/go-multi…
Browse files Browse the repository at this point in the history
…error`
  • Loading branch information
bodgit committed Nov 18, 2024
1 parent 6558fc3 commit 4de17c8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/andybalholm/brotli v1.1.1
github.com/bodgit/plumbing v1.3.0
github.com/bodgit/windows v1.0.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/klauspost/compress v1.17.11
github.com/pierrec/lz4/v4 v4.1.21
Expand All @@ -19,7 +18,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hf
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
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.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
Expand Down
4 changes: 1 addition & 3 deletions internal/bcj2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"

"github.com/bodgit/sevenzip/internal/util"
"github.com/hashicorp/go-multierror"
)

type readCloser struct {
Expand Down Expand Up @@ -102,8 +101,7 @@ func (rc *readCloser) Close() error {
return errAlreadyClosed
}

//nolint:lll
if err := multierror.Append(rc.main.Close(), rc.call.Close(), rc.jump.Close(), rc.rd.Close()).ErrorOrNil(); err != nil {
if err := errors.Join(rc.main.Close(), rc.call.Close(), rc.jump.Close(), rc.rd.Close()); err != nil {
return fmt.Errorf("bcj2: error closing: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/deflate/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/bodgit/sevenzip/internal/util"
"github.com/hashicorp/go-multierror"
"github.com/klauspost/compress/flate"
)

Expand All @@ -30,7 +29,7 @@ func (rc *readCloser) Close() error {
return errAlreadyClosed
}

if err := multierror.Append(rc.fr.Close(), rc.c.Close()).ErrorOrNil(); err != nil {
if err := errors.Join(rc.fr.Close(), rc.c.Close()); err != nil {
return fmt.Errorf("deflate: error closing: %w", err)
}

Expand Down
33 changes: 22 additions & 11 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/bodgit/plumbing"
"github.com/bodgit/sevenzip/internal/pool"
"github.com/bodgit/sevenzip/internal/util"
"github.com/hashicorp/go-multierror"
"go4.org/readerutil"
)

Expand Down Expand Up @@ -202,7 +201,7 @@ func OpenReaderWithPassword(name, password string) (*ReadCloser, error) {

info, err := f.Stat()
if err != nil {
err = multierror.Append(err, f.Close())
err = errors.Join(err, f.Close())

return nil, fmt.Errorf("sevenzip: error retrieving file info: %w", err)
}
Expand All @@ -222,22 +221,28 @@ func OpenReaderWithPassword(name, password string) (*ReadCloser, error) {
break
}

errs := make([]error, 0, len(files)+1)
errs = append(errs, err)

for _, file := range files {
err = multierror.Append(err, file.Close())
errs = append(errs, file.Close())
}

return nil, fmt.Errorf("sevenzip: error opening: %w", err)
return nil, fmt.Errorf("sevenzip: error opening: %w", errors.Join(errs...))
}

files = append(files, f)

info, err = f.Stat()
if err != nil {
errs := make([]error, 0, len(files)+1)
errs = append(errs, err)

for _, file := range files {
err = multierror.Append(err, file.Close())
errs = append(errs, file.Close())
}

return nil, fmt.Errorf("sevenzip: error retrieving file info: %w", err)
return nil, fmt.Errorf("sevenzip: error retrieving file info: %w", errors.Join(errs...))
}

sr = append(sr, io.NewSectionReader(f, 0, info.Size()))
Expand All @@ -251,11 +256,14 @@ func OpenReaderWithPassword(name, password string) (*ReadCloser, error) {
r.p = password

if err := r.init(reader, size); err != nil {
errs := make([]error, 0, len(files)+1)
errs = append(errs, err)

for _, file := range files {
err = multierror.Append(err, file.Close())
errs = append(errs, file.Close())
}

return nil, fmt.Errorf("sevenzip: error initialising: %w", err)
return nil, fmt.Errorf("sevenzip: error initialising: %w", errors.Join(errs...))
}

r.f = files
Expand Down Expand Up @@ -465,7 +473,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) (err error) {
}

defer func() {
err = multierror.Append(err, fr.Close()).ErrorOrNil()
err = errors.Join(err, fr.Close())
}()

if header, err = readEncodedHeader(util.ByteReadCloser(fr)); err != nil {
Expand Down Expand Up @@ -551,11 +559,14 @@ func (rc *ReadCloser) Volumes() []string {
}

// Close closes the 7-zip file or volumes, rendering them unusable for I/O.
func (rc *ReadCloser) Close() (err error) {
func (rc *ReadCloser) Close() error {
errs := make([]error, 0, len(rc.f))

for _, f := range rc.f {
err = multierror.Append(err, f.Close()).ErrorOrNil()
errs = append(errs, f.Close())
}

err := errors.Join(errs...)
if err != nil {
err = fmt.Errorf("sevenzip: error closing: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/bodgit/sevenzip"
"github.com/bodgit/sevenzip/internal/util"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -66,7 +65,7 @@ func extractArchive(tb testing.TB, r *sevenzip.ReadCloser, stream int, h hash.Ha
}

defer func() {
err = multierror.Append(err, rc.Close()).ErrorOrNil()
err = errors.Join(err, rc.Close())
}()

if err = extractFile(tb, fn(rc), h, f); err != nil {
Expand Down Expand Up @@ -467,7 +466,7 @@ func benchmarkArchiveNaiveParallel(b *testing.B, file string, workers int) {
}

defer func() {
err = multierror.Append(err, rc.Close()).ErrorOrNil()
err = errors.Join(err, rc.Close())
}()

return extractFile(b, rc, crc32.NewIEEE(), f)
Expand Down

0 comments on commit 4de17c8

Please sign in to comment.