Skip to content

Commit

Permalink
feat: remove multierr in favor of types
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Jun 27, 2023
1 parent cdae976 commit 5d84564
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ linters:
#- contextcheck # check the function whether to use a non-inherited context [fast: false, auto-fix: false]
#- cyclop # checks function and package cyclomatic complexity [fast: false, auto-fix: false]
- decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false]
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
#- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
#- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- durationcheck # check for two durations multiplied together [fast: false, auto-fix: false]
Expand Down
28 changes: 15 additions & 13 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@ import (
"github.com/foomo/fender/config"
"github.com/foomo/fender/fend"
"github.com/foomo/fender/rule"
"go.uber.org/multierr"
)

type Error struct {
cause error
FendErrs []*fend.Error
}

func NewError(cause error) *Error {
func NewError(fendErrs ...*fend.Error) *Error {
return &Error{
cause: cause,
FendErrs: fendErrs,
}
}

func NewFendError(name string, cause error) *Error {
return NewError(fend.NewError(name, cause))
func NewFendError(name string, ruleErrs ...*rule.Error) *Error {
return NewError(fend.NewError(name, ruleErrs...))
}

func NewFendRuleError(name string, ruleName rule.Name, ruleMeta ...string) *Error {
return NewFendError(name, rule.NewError(ruleName, ruleMeta...))
}

func (e *Error) Error() string {
errs := multierr.Errors(e.cause)
causes := make([]string, len(errs))
for i, cause := range errs {
causes := make([]string, len(e.FendErrs))
for i, cause := range e.FendErrs {
causes[i] = cause.Error()
}
return strings.Join(causes, config.DelimiterFend)
Expand All @@ -45,12 +43,16 @@ func (e *Error) First() error {
}

func (e *Error) Errors() []error {
return multierr.Errors(e.cause)
causes := make([]error, len(e.FendErrs))
for i, fendErr := range e.FendErrs {
causes[i] = fendErr
}
return causes
}

func (e *Error) Unwrap() error {
return e.cause
}
// func (e *Error) Unwrap() error {
// return e.cause
// }

func AsError(err error) *Error {
var fendErr *Error
Expand Down
42 changes: 22 additions & 20 deletions fend/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,54 @@ import (

"github.com/foomo/fender/config"
"github.com/foomo/fender/rule"
"go.uber.org/multierr"
)

type Error struct {
name string
cause error
Path string
RuleErrs []*rule.Error
}

func NewError(name string, cause error) *Error {
func NewError(path string, ruleErrs ...*rule.Error) *Error {
return &Error{
name: name,
cause: cause,
Path: path,
RuleErrs: ruleErrs,
}
}

func NewRuleError(name string, ruleName rule.Name, meta ...string) *Error {
func NewRuleError(path string, ruleName rule.Name, meta ...string) *Error {
return &Error{
name: name,
cause: rule.NewError(ruleName, meta...),
Path: path,
RuleErrs: []*rule.Error{rule.NewError(ruleName, meta...)},
}
}

func (e *Error) Name() string {
return e.name
return e.Path
}

func (e *Error) Error() string {
var ret string
errs := multierr.Errors(e.cause)
causes := make([]string, len(errs))
for i, cause := range errs {
causes[i] = cause.Error()
causes := make([]string, len(e.RuleErrs))
for i, ruleErr := range e.RuleErrs {
causes[i] = ruleErr.Error()
}
if e.name != "" {
ret += e.name + config.DelimiterFendName
if e.Path != "" {
ret += e.Path + config.DelimiterFendName
}
return ret + strings.Join(causes, config.DelimiterRule)
}

func (e *Error) Errors() []error {
return multierr.Errors(e.cause)
causes := make([]error, len(e.RuleErrs))
for i, ruleErr := range e.RuleErrs {
causes[i] = ruleErr
}
return causes
}

func (e *Error) Unwrap() error {
return e.cause
}
// func (e *Error) Unwrap() error {
// return e.rule
// }

func AsError(err error) *Error {
var fendErr *Error
Expand Down
13 changes: 6 additions & 7 deletions fend/fend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"errors"

"github.com/foomo/fender/rule"
"go.uber.org/multierr"
)

type Fend func(ctx context.Context, mode Mode) error

func fend[T any](ctx context.Context, mode Mode, meta string, value T, rules ...rule.Rule[T]) error {
var causes error
var causes []*rule.Error
for _, r := range rules {
err := r(ctx, value)
if errors.Is(err, rule.ErrBreak) {
break
} else if e, ok := err.(*rule.Error); ok { //nolint:errorlint
causes = multierr.Append(causes, e)
causes = append(causes, e)
// break if we only want the first error
if mode == ModeFirst {
break
Expand All @@ -27,19 +26,19 @@ func fend[T any](ctx context.Context, mode Mode, meta string, value T, rules ...
}
}
if causes != nil {
return NewError(meta, causes)
return NewError(meta, causes...)
}
return nil
}

func fendDynamic(ctx context.Context, mode Mode, meta string, rules ...rule.DynamicRule) error {
var causes error
var causes []*rule.Error
for _, r := range rules {
err := r(ctx)
if errors.Is(err, rule.ErrBreak) {
break
} else if e, ok := err.(*rule.Error); ok { //nolint:errorlint
causes = multierr.Append(causes, e)
causes = append(causes, e)
// break if we only want the first error
if mode == ModeFirst {
break
Expand All @@ -49,7 +48,7 @@ func fendDynamic(ctx context.Context, mode Mode, meta string, rules ...rule.Dyna
}
}
if causes != nil {
return NewError(meta, causes)
return NewError(meta, causes...)
}
return nil
}
3 changes: 1 addition & 2 deletions fend/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/foomo/fender/rule"
"go.uber.org/multierr"
)

func Union[T any](rules ...Rules[T]) rule.Rule[T] {
Expand All @@ -14,7 +13,7 @@ func Union[T any](rules ...Rules[T]) rule.Rule[T] {
var e2 error
for _, r2 := range r {
if err := r2(ctx, v); err != nil {
e2 = multierr.Append(e2, err)
e2 = err
}
}
if e2 != nil {
Expand Down
7 changes: 3 additions & 4 deletions fender.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/foomo/fender/fend"
"go.uber.org/multierr"
)

func All(ctx context.Context, fends ...fend.Fend) error {
Expand All @@ -20,12 +19,12 @@ func AllFirst(ctx context.Context, fends ...fend.Fend) error {
}

func Mode(ctx context.Context, mode fend.Mode, fends ...fend.Fend) error {
var cause error
var cause []*fend.Error
for _, validator := range fends {
err := validator(ctx, mode)
if e, ok := err.(*fend.Error); ok { //nolint:errorlint
// append error
cause = multierr.Append(cause, e)
cause = append(cause, e)
// break if we only want the first errors
if mode == fend.ModeFirst || mode == fend.ModeAllFirst {
break
Expand All @@ -35,7 +34,7 @@ func Mode(ctx context.Context, mode fend.Mode, fends ...fend.Fend) error {
}
}
if cause != nil {
return NewError(cause)
return NewError(cause...)
}
return nil
}

0 comments on commit 5d84564

Please sign in to comment.