Skip to content

Commit

Permalink
Merge pull request #11 from tetratelabs/errors
Browse files Browse the repository at this point in the history
Remove multierror dependency
  • Loading branch information
nacx authored Feb 16, 2024
2 parents 70f7053 + cad5ee6 commit 5ddf24d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 36 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ jobs:
strategy:
matrix:
go-version:
- 1.16.x
- 1.17.x
- 1.18.x
- 1.19.x
- 1.20.x
- 1.21.x
- 1.22.x
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- run: go test ./...
Expand Down
13 changes: 5 additions & 8 deletions example_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"os"

"github.com/spf13/pflag"
"github.com/tetratelabs/multierror"

"github.com/tetratelabs/run"
"github.com/tetratelabs/run/pkg/signal"
)
Expand Down Expand Up @@ -75,18 +73,17 @@ func (p *PersonService) FlagSet() *pflag.FlagSet {
// Validate implements run.Config and thus its configuration and flag handling
// is automatically registered when adding the service to Group.
func (p PersonService) Validate() error {
var err error
var errs []error
if p.name == "" {
err = multierror.Append(err, errors.New("invalid name provided"))
errs = append(errs, errors.New("invalid name provided"))
}
if p.age < 18 {
err = multierror.Append(err, errors.New("invalid age provided, we don't serve minors"))
errs = append(errs, errors.New("invalid age provided, we don't serve minors"))
}
if p.age > 110 {
err = multierror.Append(err, errors.New("faking it? or life expectancy assumptions surpassed by future healthcare system"))
errs = append(errs, errors.New("faking it? or life expectancy assumptions surpassed by future healthcare system"))
}

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

// PreRun implements run.PreRunner and thus this method is run at the pre-run
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ go 1.17
require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/spf13/pflag v1.0.5
github.com/tetratelabs/multierror v1.1.0
github.com/tetratelabs/telemetry v0.7.1
)

require github.com/hashicorp/errwrap v1.0.0 // indirect
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/tetratelabs/multierror v1.1.0 h1:cKmV/Pbf42K5wp8glxa2YIausbxIraPN8fzru9Pn1Cg=
github.com/tetratelabs/multierror v1.1.0/go.mod h1:kH3SzI/z+FwEbV9bxQDx4GiIgE2djuyb8wiB2DaUBnY=
github.com/tetratelabs/telemetry v0.7.1 h1:IiDiiZgShKlHjPFgCAE6ZD4URH3r8yj7SDAEN/ImHYA=
github.com/tetratelabs/telemetry v0.7.1/go.mod h1:jDUcf1A2u4F5V1io5RdipM/bKz/hFCsx/RAgGopC37s=
14 changes: 6 additions & 8 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

color "github.com/logrusorgru/aurora"
"github.com/spf13/pflag"
"github.com/tetratelabs/multierror"
"github.com/tetratelabs/telemetry"

"github.com/tetratelabs/run/pkg/log"
Expand Down Expand Up @@ -339,9 +338,8 @@ func (g *Group) RunConfig(args ...string) (err error) {
g.HelpText = strings.ReplaceAll(g.HelpText, BinaryName, os.Args[0])

defer func() {
if err != nil && err != ErrBailEarlyRequest {
if err != nil && !errors.Is(err, ErrBailEarlyRequest) {
g.Logger.Error("unexpected exit", err)
err = multierror.SetFormatter(err, multierror.ListFormatFunc)
}
}()

Expand Down Expand Up @@ -464,6 +462,7 @@ func (g *Group) RunConfig(args ...string) (err error) {
}

// Validate Config inputs
var errs []error
for idx, cfg := range g.c {
func(itemNr int, cfg Config) {
// a Config might have been de-registered during Run
Expand All @@ -482,14 +481,14 @@ func (g *Group) RunConfig(args ...string) (err error) {
defer l.Debug("validate-exit", debugLogError(vErr)...)
vErr = cfg.Validate()
if vErr != nil {
err = multierror.Append(err, vErr)
errs = append(errs, vErr)
}
}(idx+1, cfg)
}

// exit on at least one Validate error
if err != nil {
return err
if len(errs) > 0 {
return fmt.Errorf("%d errors occured:\n%w", len(errs), errors.Join(errs...))
}

// log binary name and version
Expand Down Expand Up @@ -539,7 +538,7 @@ func (g *Group) Run(args ...string) (err error) {
if !g.configured {
// run config registration and flag parsing stages
if err = g.RunConfig(args...); err != nil {
if err == ErrBailEarlyRequest {
if errors.Is(err, ErrBailEarlyRequest) {
return nil
}
return err
Expand Down Expand Up @@ -571,7 +570,6 @@ func (g *Group) Run(args ...string) (err error) {
}
// actual fatal error
g.Logger.Error("unexpected exit", err)
err = multierror.SetFormatter(err, multierror.ListFormatFunc)
}()

// call our Initializer (again)
Expand Down
8 changes: 1 addition & 7 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"testing"
"time"

"github.com/tetratelabs/multierror"

"github.com/tetratelabs/run"
"github.com/tetratelabs/run/pkg/test"
)
Expand Down Expand Up @@ -114,11 +112,7 @@ func TestRunGroupMultiErrorHandling(t *testing.T) {
err1 = errors.New("cfg1 failed")
err2 = errors.New("cfg2 failed")
err3 = errors.New("cfg3 failed")

mErr = multierror.SetFormatter(
multierror.Append(nil, err1, err2, err3),
multierror.ListFormatFunc,
)
mErr = fmt.Errorf("3 errors occured:\n%w", errors.Join(err1, err2, err3))

cfg1 = failingConfig{e: err1}
cfg2 = failingConfig{e: err2}
Expand Down

0 comments on commit 5ddf24d

Please sign in to comment.