Skip to content

Commit

Permalink
vterrors
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <[email protected]>
  • Loading branch information
Andrew Mason committed May 23, 2023
1 parent 098f0d2 commit dee064f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions go/viperutil/internal/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package sync

import (
"context"
"errors"
"fmt"
"sync"
"time"
Expand All @@ -28,6 +27,8 @@ import (
"github.com/spf13/viper"

"vitess.io/vitess/go/viperutil/internal/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// Viper is a wrapper around a pair of viper.Viper instances to provide config-
Expand Down Expand Up @@ -89,7 +90,7 @@ func (v *Viper) Set(key string, value any) {

// ErrDuplicateWatch is returned when Watch is called on a synced Viper which
// has already started a watch.
var ErrDuplicateWatch = errors.New("duplicate watch")
var ErrDuplicateWatch = vterrors.New(vtrpc.Code_FAILED_PRECONDITION, "duplicate watch")

// Watch starts watching the config used by the passed-in Viper. Before starting
// the watch, the synced viper will perform an initial read and load from disk
Expand Down Expand Up @@ -121,7 +122,7 @@ var ErrDuplicateWatch = errors.New("duplicate watch")
// to ensure the config file can be read in properly.
func (v *Viper) Watch(ctx context.Context, static *viper.Viper, minWaitInterval time.Duration) (cancel context.CancelFunc, err error) {
if v.watchingConfig {
return nil, fmt.Errorf("%w: viper is already watching %s", ErrDuplicateWatch, v.disk.ConfigFileUsed())
return nil, vterrors.Wrapf(ErrDuplicateWatch, "%s: viper is already watching %s", ErrDuplicateWatch.Error(), v.disk.ConfigFileUsed())
}

ctx, cancel = context.WithCancel(ctx)
Expand Down
7 changes: 4 additions & 3 deletions go/viperutil/internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package value

import (
"errors"
"fmt"

"github.com/spf13/pflag"
"github.com/spf13/viper"

"vitess.io/vitess/go/viperutil/internal/registry"
"vitess.io/vitess/go/viperutil/internal/sync"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// Registerable is the subset of the interface exposed by Values (which is
Expand Down Expand Up @@ -59,7 +60,7 @@ func (val *Base[T]) Get() T { return val.BoundGetFunc(val.Key()) }

// ErrNoFlagDefined is returned when a Value has a FlagName set, but the given
// FlagSet does not define a flag with that name.
var ErrNoFlagDefined = errors.New("flag not defined")
var ErrNoFlagDefined = vterrors.New(vtrpc.Code_INVALID_ARGUMENT, "flag not defined")

// Flag is part of the Registerable interface. If the given flag set has a flag
// with the name of this value's configured flag, that flag is returned, along
Expand All @@ -75,7 +76,7 @@ func (val *Base[T]) Flag(fs *pflag.FlagSet) (*pflag.Flag, error) {

flag := fs.Lookup(val.FlagName)
if flag == nil {
return nil, fmt.Errorf("%w with name %s (for key %s)", ErrNoFlagDefined, val.FlagName, val.Key())
return nil, vterrors.Wrapf(ErrNoFlagDefined, "%s with name %s (for key %s)", ErrNoFlagDefined.Error(), val.FlagName, val.Key())
}

return flag, nil
Expand Down

0 comments on commit dee064f

Please sign in to comment.