Skip to content

Commit

Permalink
fix: deduplicate any errors during config --load (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow authored Oct 15, 2024
1 parent 2b1b1d1 commit f538a90
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
39 changes: 17 additions & 22 deletions config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,7 @@ func loadAllConfigs(cmd *cobra.Command, fangsCfg fangs.Config, allConfigs []any)
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
if isProfileError(err) {
if hasProfileError(errs) {
// only report profile errors once
continue
}
// directly append the profile error, since this will be repeated for each configuration object and is applicable to all
errs = append(errs, err)
} else {
errs = append(errs, fmt.Errorf("error loading config '%s.%s': %w", t.PkgPath(), t.Name(), err))
}
errs = appendConfigLoadError(errs, t, err)
}
}
if len(errs) == 0 {
Expand Down Expand Up @@ -204,21 +195,25 @@ func summarizeLocations(fangsCfg fangs.Config, onlySuffix string) string {
return out
}

// fangs load may result in each configuration object being attempted to load with a failed profile for different reasons
func isProfileError(err error) bool {
// appendConfigLoadError appends errors including originating struct, but deduplicates identical errors that occur across multiple load calls
func appendConfigLoadError(errs []error, t reflect.Type, err error) []error {
if err == nil {
return false
return errs
}
msg := err.Error()
return strings.Contains(msg, "not found in any configuration files") ||
strings.Contains(msg, "profile not found")
}

func hasProfileError(errs []error) bool {
for _, e := range errs {
if isProfileError(e) {
return true
// remove configuration object source when we get the same error from multiple sources
for i, e := range errs {
// already have this error, don't append
if e.Error() == msg {
return errs
}
// if we have an identical wrapped error, this occurred when loading multiple configurations so just show the error
if e, ok := e.(interface{ Unwrap() error }); ok {
if e.Unwrap().Error() == msg {
errs[i] = err
return errs
}
}
}
return false
return append(errs, fmt.Errorf("error loading config '%s.%s': %w", t.PkgPath(), t.Name(), err))
}
54 changes: 54 additions & 0 deletions config_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clio
import (
"fmt"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -228,3 +229,56 @@ func Test_ConsolidateProfileErrors(t *testing.T) {
})
}
}

func Test_appendConfigLoadError(t *testing.T) {
type ty struct{}
typ := reflect.TypeOf(ty{})
typErrMsg := fmt.Sprintf("error loading config '%s.%s': ", typ.PkgPath(), typ.Name()) + "%w"

tests := []struct {
name string
errs []error
expected []error
}{
{
name: "no duplicates",
errs: []error{
fmt.Errorf("some error"),
fmt.Errorf("existing error"),
fmt.Errorf("existing error2"),
fmt.Errorf("new error"),
},
expected: []error{
fmt.Errorf(typErrMsg, fmt.Errorf("some error")),
fmt.Errorf(typErrMsg, fmt.Errorf("existing error")),
fmt.Errorf(typErrMsg, fmt.Errorf("existing error2")),
fmt.Errorf(typErrMsg, fmt.Errorf("new error")),
},
},
{
name: "duplicates",
errs: []error{
fmt.Errorf("some error"),
fmt.Errorf("duplicate error"),
fmt.Errorf("existing error"),
fmt.Errorf("duplicate error"),
fmt.Errorf("duplicate error"),
},
expected: []error{
fmt.Errorf(typErrMsg, fmt.Errorf("some error")),
fmt.Errorf("duplicate error"), // type should be removed and error not duplicated, since multiple types report the same error
fmt.Errorf(typErrMsg, fmt.Errorf("existing error")),
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var got []error
for _, err := range test.errs {
got = appendConfigLoadError(got, typ, err)
}
require.ElementsMatch(t, test.expected, got)
})
}
}

0 comments on commit f538a90

Please sign in to comment.