Skip to content

Commit

Permalink
refactor: replace go-multierror with stdlib methods
Browse files Browse the repository at this point in the history
Go 1.20 added support for wrapping multiple errors.

Replace multierror libs with stdlib methods
  • Loading branch information
kruskall committed Nov 17, 2023
1 parent dc9719a commit d23e398
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 43 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ require (
github.com/elastic/go-ucfg v0.8.5
github.com/fatih/color v1.13.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/hashicorp/go-multierror v1.1.1
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/magefile/mage v1.13.0
github.com/mattn/go-colorable v0.1.12
github.com/mitchellh/hashstructure v1.1.0
Expand All @@ -36,9 +34,9 @@ require (
github.com/elastic/go-windows v1.0.1 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcchavezs/porto v0.1.0 // indirect
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/markbates/pkger v0.17.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0 h1:OggOMmdI0JLwg1FkOKH9S7fVHF0oEm8PX6S8kAdpOps=
github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jcchavezs/porto v0.1.0 h1:Xmxxn25zQMmgE7/yHYmh19KcItG81hIwfbEEFnd6w/Q=
Expand Down
13 changes: 6 additions & 7 deletions kibana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -33,8 +34,6 @@ import (
"path"
"strings"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/transport/httpcommon"
Expand Down Expand Up @@ -84,15 +83,15 @@ func extractError(result []byte) error {
if err := json.Unmarshal(result, &kibanaResult); err != nil {
return fmt.Errorf("error extracting JSON for error response: %w", err)
}
var errs multierror.Errors
var errs []error
if kibanaResult.Message != "" {
for _, err := range kibanaResult.Attributes.Objects {
errs = append(errs, fmt.Errorf("id: %s, message: %s", err.ID, err.Error.Message))
}
if errs == nil {
if len(errs) == 0 {
return fmt.Errorf("%s", kibanaResult.Message)
}
return fmt.Errorf("%s: %w", kibanaResult.Message, errs.Err())
return fmt.Errorf("%s: %w", kibanaResult.Message, errors.Join(errs...))

}
return nil
Expand All @@ -118,11 +117,11 @@ func extractMessage(result []byte) error {
}

if !kibanaResult.Success {
var errs multierror.Errors
var errs []error
for _, err := range kibanaResult.Errors {
errs = append(errs, fmt.Errorf("error: %s, asset ID=%s; asset type=%s; references=%+v", err.Error.Type, err.ID, err.Type, err.Error.References))
}
return errs.Err()
return errors.Join(errs...)
}

return nil
Expand Down
7 changes: 3 additions & 4 deletions logp/cfgwarn/removed.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
package cfgwarn

import (
"errors"
"fmt"
"strings"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/config"
)

func checkRemovedSettings(cfg *config.C, settings ...string) error {
var errs multierror.Errors
var errs []error
for _, setting := range settings {
if err := checkRemovedSetting(cfg, setting); err != nil {
errs = append(errs, err)
}
}

return errs.Err()
return errors.Join(errs...)
}

func checkRemovedSetting(cfg *config.C, setting string) error {
Expand Down
11 changes: 5 additions & 6 deletions logp/cfgwarn/removed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"testing"

"github.com/joeshaw/multierror"
"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestRemovedSettings(t *testing.T) {
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
}),
expected: multierror.Errors{errors.New("setting 'hello' has been removed")}.Err(),
expected: errors.Join(errors.New("setting 'hello' has been removed")),
},
{
name: "multiple obsolete settings",
Expand All @@ -102,10 +101,10 @@ func TestRemovedSettings(t *testing.T) {
"hello.world": "ok",
"bad": "true",
}),
expected: multierror.Errors{
expected: errors.Join(
errors.New("setting 'hello' has been removed"),
errors.New("setting 'bad' has been removed"),
}.Err(),
),
},
{
name: "multiple obsolete settings not on first level",
Expand All @@ -114,10 +113,10 @@ func TestRemovedSettings(t *testing.T) {
"filebeat.prospectors": "ok",
"filebeat.config.prospectors": map[string]interface{}{"ok": "ok1"},
}),
expected: multierror.Errors{
expected: errors.Join(
errors.New("setting 'filebeat.config.prospectors' has been removed"),
errors.New("setting 'filebeat.prospectors' has been removed"),
}.Err(),
),
},
}

Expand Down
15 changes: 7 additions & 8 deletions logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package logp

import (
"errors"
"flag"
"fmt"
"io/ioutil"
Expand All @@ -28,8 +29,6 @@ import (
"sync/atomic"
"unsafe"

"github.com/hashicorp/go-multierror"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
Expand Down Expand Up @@ -325,22 +324,22 @@ func (m multiCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *za

// Write writes the entry to each core.
func (m multiCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
var errs error
var errs []error
for _, core := range m.cores {
if err := core.Write(entry, fields); err != nil {
errs = multierror.Append(errs, err)
errs = append(errs, err)
}
}
return errs
return errors.Join(errs...)
}

// Sync syncs each core.
func (m multiCore) Sync() error {
var errs error
var errs []error
for _, core := range m.cores {
if err := core.Sync(); err != nil {
errs = multierror.Append(errs, err)
errs = append(errs, err)
}
}
return errs
return errors.Join(errs...)
}
9 changes: 4 additions & 5 deletions transport/tlscommon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package tlscommon

import (
"crypto/tls"
"errors"
"sync"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/logp/cfgwarn"
)

Expand Down Expand Up @@ -51,7 +50,7 @@ func LoadTLSConfig(config *Config) (*TLSConfig, error) {
return nil, nil
}

fail := multierror.Errors{}
var fail []error
logFail := func(es ...error) {
for _, e := range es {
if e != nil {
Expand All @@ -72,8 +71,8 @@ func LoadTLSConfig(config *Config) (*TLSConfig, error) {
logFail(errs...)

// fail, if any error occurred when loading certificate files
if err = fail.Err(); err != nil {
return nil, err
if len(fail) != 0 {
return nil, errors.Join(fail...)
}

certs := make([]tls.Certificate, 0)
Expand Down
9 changes: 4 additions & 5 deletions transport/tlscommon/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package tlscommon

import (
"crypto/tls"
"errors"
"fmt"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/config"
)

Expand All @@ -46,7 +45,7 @@ func LoadTLSServerConfig(config *ServerConfig) (*TLSConfig, error) {
return nil, nil
}

fail := multierror.Errors{}
var fail []error
logFail := func(es ...error) {
for _, e := range es {
if e != nil {
Expand All @@ -72,8 +71,8 @@ func LoadTLSServerConfig(config *ServerConfig) (*TLSConfig, error) {
logFail(errs...)

// fail, if any error occurred when loading certificate files
if err = fail.Err(); err != nil {
return nil, err
if len(fail) != 0 {
return nil, errors.Join(fail...)
}

certs := make([]tls.Certificate, 0)
Expand Down

0 comments on commit d23e398

Please sign in to comment.