Added fixes for crashing on bad filter

* Does not save profile in the SetFilter function.
* Saves profile on exit (exit from main loop).
* Clears filter string if it is found to be invalid in the Apply
  function.
master
Adam Hayes 3 years ago
parent bff66ef176
commit 635e467e7a
  1. 1
      cmd/mop/main.go
  2. 12
      filter.go
  3. 1
      profile.go

@ -143,4 +143,5 @@ func main() {
profile := mop.NewProfile(*profileName) profile := mop.NewProfile(*profileName)
mainLoop(screen, profile) mainLoop(screen, profile)
profile.Save()
} }

@ -50,13 +50,21 @@ func (filter *Filter) Apply(stocks []Stock) []Stock {
result, err := filter.profile.filterExpression.Evaluate(values) result, err := filter.profile.filterExpression.Evaluate(values)
if err != nil { if err != nil {
panic(err) // The filter isn't working, so reset to no filter.
filter.profile.Filter = ""
// Return an empty list. The next main loop cycle will
// show unfiltered.
return filteredStocks
} }
truthy, ok := result.(bool) truthy, ok := result.(bool)
if !ok { if !ok {
panic("Expression `" + filter.profile.Filter + "` should return a boolean value") // The filter isn't working, so reset to no filter.
filter.profile.Filter = ""
// Return an empty list. The next main loop cycle will
// show unfiltered.
return filteredStocks
} }
if truthy { if truthy {

@ -142,5 +142,4 @@ func (profile *Profile) SetFilter(filter string) {
} }
profile.Filter = filter profile.Filter = filter
profile.Save()
} }

Loading…
Cancel
Save