Skip to content

Commit

Permalink
Consolidate error messages for invalid frameworks (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney authored Mar 4, 2024
1 parent 524f3a0 commit d73e0d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
51 changes: 32 additions & 19 deletions modes/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@ import (
)

var (
ErrLoadRequiresName = errors.New("'load' requires a file name")
ErrNoFrameworkSelected = errors.New("no frameworks selected")
ErrNoModel = errors.New("no model loaded")
ErrLoadRequiresName = errors.New("'load' requires a file name")
ErrNoModel = errors.New("no model loaded")
)

type ErrInvalidFramework struct {
Name string
}

func (e ErrInvalidFramework) Error() string {
return fmt.Sprintf("%q is not a valid framework", e.Name)
}

type ErrUnrecognizedCommand struct {
Command string
}
Expand All @@ -44,6 +35,15 @@ func (e ErrUnrecognizedCommand) Error() string {
return fmt.Sprintf("unrecognized command: %q", e.Command)
}

type ErrNoFrameworkSelected struct {
ValidFrameworks []string
}

func (e ErrNoFrameworkSelected) Error() string {
valid := strings.Join(e.ValidFrameworks, ", ")
return fmt.Sprintf("no framework selected; expected one of %q or \"all\"", valid)
}

type command struct {
description string
method func(string) error
Expand Down Expand Up @@ -153,7 +153,21 @@ func (s *Shell) runCommand(c string) (err error) {
return &ErrUnrecognizedCommand{Command: cmd}
}

func (s Shell) printActiveFrameworks() {
fmt.Print("active frameworks: ")
for name := range s.activeFrameworks {
fmt.Printf("%s ", name)
}
fmt.Println()
}

func (s *Shell) cmdFramework(fNames string) (err error) {
// If no frameworks were specified, then return list of active frameworks
if len(fNames) == 0 {
s.printActiveFrameworks()
return
}

names := strings.Split(fNames, " ")
sort.Strings(names)

Expand All @@ -166,24 +180,23 @@ func (s *Shell) cmdFramework(fNames string) (err error) {

for _, name := range names {
if !s.settings.ActiveFrameworks.Exists(name) {
err = &ErrInvalidFramework{Name: name}
err = fmt.Errorf("%w. Valid values: %v", err, s.settings.ActiveFrameworks.Names())
err = runoptions.ErrInvalidFrameworkName{
Name: name,
ValidFrameworks: s.settings.ActiveFrameworks.Names(),
}
return
}

s.activeFrameworks[name] = true
}

// Should not be possible..
if len(s.activeFrameworks) == 0 {
err = fmt.Errorf("%w. Valid values: %v", ErrNoFrameworkSelected, framework.ValidFrameworks)
err = ErrNoFrameworkSelected{ValidFrameworks: framework.ValidFrameworks}
return
}

fmt.Print("active frameworks: ")
for name := range s.activeFrameworks {
fmt.Printf("%s ", name)
}
fmt.Println()
s.printActiveFrameworks()

return
}
Expand Down
3 changes: 2 additions & 1 deletion util/frameworkutil/frameworkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func CreateFrameworks(settings *cli.Settings, names []string) (list framework.Li
fw, err = vanilla_actr.New(settings.TempPath)

default:
chalk.PrintErrStr("unknown framework:", f)
err = runoptions.ErrInvalidFrameworkName{Name: f}
chalk.PrintErr(err)
continue
}

Expand Down
9 changes: 7 additions & 2 deletions util/runoptions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ func (e ErrFrameworkNotActive) Error() string {
}

type ErrInvalidFrameworkName struct {
Name string
Name string
ValidFrameworks []string
}

func (e ErrInvalidFrameworkName) Error() string {
return fmt.Sprintf("invalid framework name: %q; expected one of %q", e.Name, strings.Join(ValidFrameworks, ", "))
valid := strings.Join(e.ValidFrameworks, ", ")
if len(valid) == 0 {
valid = strings.Join(ValidNamedFrameworks(), ", ")
}
return fmt.Sprintf("invalid framework name: %q; expected one of %q or \"all\"", e.Name, valid)
}

type ErrInvalidLogLevel struct {
Expand Down
7 changes: 5 additions & 2 deletions util/runoptions/runoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ func (f *FrameworkNameList) NormalizeFrameworkList(activeFrameworks FrameworkNam
func (f FrameworkNameList) VerifyFrameworkList(activeFrameworks FrameworkNameList) (err error) {
for _, name := range f {
if !IsValidFramework(name) {
err = &ErrInvalidFrameworkName{Name: name}
err = ErrInvalidFrameworkName{
Name: name,
ValidFrameworks: activeFrameworks,
}
return
}

// we have a valid name, check if it is active
if !slices.Contains(activeFrameworks, name) {
err = &ErrFrameworkNotActive{Name: name}
err = ErrFrameworkNotActive{Name: name}
return
}
}
Expand Down

0 comments on commit d73e0d2

Please sign in to comment.