Skip to content

Commit

Permalink
chore: unify error displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-spike committed Oct 30, 2023
1 parent 94dffc3 commit 72a66d1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion asconf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func ParseFmtString(in string) (f Format, err error) {
f = AsConfig
default:
f = Invalid
err = fmt.Errorf("%w: %s", ErrInvalidFormat, f)
err = fmt.Errorf("%w: %s", ErrInvalidFormat, in)
}

return
Expand Down
28 changes: 10 additions & 18 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
var (
errTooManyArguments = fmt.Errorf("expected a maximum of %d arguments", convertArgMax)
errFileNotExist = fmt.Errorf("file does not exist")
errMissingAerospikeVersion = fmt.Errorf("missing required flag '--aerospike-version'")
errInvalidAerospikeVersion = fmt.Errorf("aerospike version must be in the form <a>.<b>.<c>")
errUnsupportedAerospikeVersion = fmt.Errorf("aerospike version unsupported")
errInvalidOutput = fmt.Errorf("invalid output flag")
Expand Down Expand Up @@ -114,8 +115,6 @@ func newConvertCmd() *cobra.Command {
if !force {
err = conf.Validate()
if err != nil {
// validation errors are not user errors, so don't print usage
cmd.SilenceUsage = true
return err
}
}
Expand Down Expand Up @@ -167,30 +166,26 @@ func newConvertCmd() *cobra.Command {

},
PreRunE: func(cmd *cobra.Command, args []string) error {
var multiErr error

if len(args) > convertArgMax {
logger.Errorf("Expected no more than %d argument(s)", convertArgMax)
multiErr = errors.Join(multiErr, errTooManyArguments)
return errTooManyArguments
}

if len(args) > 0 {
source := args[0]
if _, err := os.Stat(source); errors.Is(err, os.ErrNotExist) {
logger.Errorf("Source file does not exist %s", source)
multiErr = errors.Join(multiErr, errFileNotExist, err)
return errors.Join(errFileNotExist, err)
}
}

// validate flags
_, err := cmd.Flags().GetString("output")
if err != nil {
multiErr = errors.Join(multiErr, err)
return err
}

force, err := cmd.Flags().GetBool("force")
if err != nil {
multiErr = errors.Join(multiErr, err)
return err
}

if !force {
Expand All @@ -199,30 +194,27 @@ func newConvertCmd() *cobra.Command {

av, err := cmd.Flags().GetString("aerospike-version")
if err != nil {
multiErr = errors.Join(multiErr, errInvalidAerospikeVersion, err)
return err
}

if !force {
if av == "" {
logger.Error("missing required flag '--aerospike-version'")
multiErr = errors.Join(multiErr, errInvalidAerospikeVersion, err)
return errors.Join(errMissingAerospikeVersion, err)
}

supported, err := asconfig.IsSupportedVersion(av)
if err != nil {
logger.Errorf("Failed to check aerospike version %s for compatibility", av)
multiErr = errors.Join(multiErr, errInvalidAerospikeVersion, err)
return errors.Join(errInvalidAerospikeVersion, err)
}

// TODO include valid versions in the error message
// asconfig lib needs a getSupportedVersions func
if !supported {
logger.Errorf("Unsupported aerospike server version: %s", av)
multiErr = errors.Join(multiErr, errUnsupportedAerospikeVersion)
return errUnsupportedAerospikeVersion
}
}

return multiErr
return nil
},
}

Expand Down
11 changes: 2 additions & 9 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,20 @@ var preTestsConvert = []preTestConvert{
arguments: []string{"./bad_file.yaml", "too_many"},
expectedErrors: []error{
errTooManyArguments,
errFileNotExist,
errInvalidAerospikeVersion,
errUnsupportedAerospikeVersion,
},
},
{
flags: []string{},
arguments: []string{"./bad_file.yaml", "too_many"},
arguments: []string{"./bad_file.yaml"},
expectedErrors: []error{
errTooManyArguments,
errFileNotExist,
errInvalidAerospikeVersion,
errUnsupportedAerospikeVersion,
},
},
{
flags: []string{},
arguments: []string{"./convert_test.go"},
expectedErrors: []error{
errInvalidAerospikeVersion,
errUnsupportedAerospikeVersion,
errMissingAerospikeVersion,
},
},
{
Expand Down
8 changes: 4 additions & 4 deletions cmd/diff.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cmd

import (
"errors"
"fmt"
"github.com/aerospike/asconfig/asconf"
"os"
"reflect"
"sort"
"strings"

"github.com/aerospike/asconfig/asconf"

"github.com/spf13/cobra"
)

Expand All @@ -17,7 +19,7 @@ const (
)

var (
errDiffConfigsDiffer = fmt.Errorf("configuration files are not equal")
errDiffConfigsDiffer = errors.Join(fmt.Errorf("configuration files are not equal"), SilentError)
errDiffTooFewArgs = fmt.Errorf("diff requires atleast %d file paths as arguments", diffArgMin)
errDiffTooManyArgs = fmt.Errorf("diff requires no more than %d file paths as arguments", diffArgMax)
)
Expand Down Expand Up @@ -120,8 +122,6 @@ func newDiffCmd() *cobra.Command {
if len(diffs) > 0 {
fmt.Printf("Differences shown from %s to %s, '<' are from file1, '>' are from file2.\n", path1, path2)
fmt.Println(strings.Join(diffs, ""))
cmd.SilenceUsage = true
cmd.SilenceErrors = true
return errDiffConfigsDiffer
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"

"github.com/aerospike/asconfig/log"

Expand Down Expand Up @@ -72,6 +74,15 @@ func newRootCmd() *cobra.Command {
res.PersistentFlags().BoolP("version", "V", false, "Version for asconfig.")
res.PersistentFlags().StringP("format", "F", "conf", "The format of the source file(s). Valid options are: yaml, yml, and conf.")

res.SilenceErrors = true
res.SilenceUsage = true

res.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
logger.Error(err)
cmd.Println(cmd.UsageString())
return errors.Join(err, SilentError)
})

return res
}

Expand All @@ -80,6 +91,14 @@ func newRootCmd() *cobra.Command {
func Execute() {
err := rootCmd.Execute()
if err != nil {
if !errors.Is(err, SilentError) {
// handle wrapped errors
errs := strings.Split(err.Error(), "\n")

for _, err := range errs {
logger.Error(err)
}
}
os.Exit(1)
}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"github.com/aerospike/asconfig/asconf"
"errors"
"path/filepath"
"strings"

"github.com/aerospike/asconfig/asconf"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -35,3 +37,5 @@ func getConfFileFormat(path string, cmd *cobra.Command) (asconf.Format, error) {

return fmt, nil
}

var SilentError = errors.New("SILENT")

0 comments on commit 72a66d1

Please sign in to comment.