Skip to content

Commit

Permalink
Improvement on texts and error behaviours on /todo settings (#110)
Browse files Browse the repository at this point in the history
* Improvement on texts and error behaviours on /todo settings

* Fix test by leveraging already implemented behaviour

* Add full stop at the end of the errors, and unify representation of on/off

* Log saving error

* Fix test
  • Loading branch information
larkox authored Sep 2, 2020
1 parent 76cd654 commit ccfcd25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
27 changes: 17 additions & 10 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
isUserError, err := handler(restOfArgs, args)
if err != nil {
if isUserError {
p.postCommandResponse(args, fmt.Sprintf("__Error: %s__\n\nRun `/todo help` for usage instructions.", err.Error()))
p.postCommandResponse(args, fmt.Sprintf("__Error: %s.__\n\nRun `/todo help` for usage instructions.", err.Error()))
} else {
p.API.LogError(err.Error())
p.postCommandResponse(args, "An unknown error occurred. Please talk to your system administrator for help.")
Expand Down Expand Up @@ -278,34 +278,41 @@ func (p *Plugin) runPopCommand(args []string, extra *model.CommandArgs) (bool, e
}

func (p *Plugin) runSettingsCommand(args []string, extra *model.CommandArgs) (bool, error) {
if len(args) != 2 {
p.postCommandResponse(extra, "invalid number of arguments")
return true, errors.New("invalid number of arguments")
if len(args) < 1 {
return true, errors.New("no setting selected")
}

if args[0] == "summary" {
if len(args) < 2 {
return true, errors.New("choose whether you want this setting `on` or `off`")
}
if len(args) > 2 {
return true, errors.New("too many arguments")
}
var responseMessage string
var err error

switch args[1] {
case "on":
err = p.saveReminderPreference(extra.UserId, true)
responseMessage = "You will start receiving daily summaries."
case "off":
err = p.saveReminderPreference(extra.UserId, false)
responseMessage = "You will stop receiving daily summaries."
default:
responseMessage = `invalid input, allowed values for "settings summary" are [on] or [off]"`
p.postCommandResponse(extra, responseMessage)
return true, errors.New("invalid argument")
responseMessage = "invalid input, allowed values for \"settings summary\" are `on` or `off`"
return true, errors.New(responseMessage)
}

if err != nil {
responseMessage = "error saving the reminder preference"
p.postCommandResponse(extra, responseMessage)
return false, err
p.API.LogDebug("runSettingsCommand: error saving the reminder preference", "error", err.Error())
return false, errors.New(responseMessage)
}

responseMessage = fmt.Sprintf("reminder preference changed to %s", args[1])
p.postCommandResponse(extra, responseMessage)
} else {
return true, fmt.Errorf("setting `%s` not recognized", args[0])
}

return false, nil
Expand Down
1 change: 1 addition & 0 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestSetttingsCommand(t *testing.T) {
apiKVSetFailed := &plugintest.API{}
apiKVSetFailed.On("SendEphemeralPost", mock.AnythingOfType("string"), mock.Anything).Return(nil)
apiKVSetFailed.On("KVSet", mock.AnythingOfType("string"), mock.Anything).Return(model.NewAppError("failed", "", nil, "", 400))
apiKVSetFailed.On("LogDebug", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("string"))

tests := []struct {
name string
Expand Down

0 comments on commit ccfcd25

Please sign in to comment.