From 635e467e7a5732b04c2ec29ad0293729e5c30bd4 Mon Sep 17 00:00:00 2001 From: Adam Hayes Date: Tue, 23 Nov 2021 22:47:53 -0500 Subject: [PATCH] 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. --- cmd/mop/main.go | 1 + filter.go | 12 ++++++++++-- profile.go | 1 - 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/mop/main.go b/cmd/mop/main.go index 3c5ff72..7c64b77 100644 --- a/cmd/mop/main.go +++ b/cmd/mop/main.go @@ -143,4 +143,5 @@ func main() { profile := mop.NewProfile(*profileName) mainLoop(screen, profile) + profile.Save() } diff --git a/filter.go b/filter.go index 4cdf61b..f79decc 100644 --- a/filter.go +++ b/filter.go @@ -50,13 +50,21 @@ func (filter *Filter) Apply(stocks []Stock) []Stock { result, err := filter.profile.filterExpression.Evaluate(values) 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) 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 { diff --git a/profile.go b/profile.go index 6bb3c1e..ce393ed 100644 --- a/profile.go +++ b/profile.go @@ -142,5 +142,4 @@ func (profile *Profile) SetFilter(filter string) { } profile.Filter = filter - profile.Save() }