From 46dbfb0caaab521b3a9e5ae4b95e55482865fb74 Mon Sep 17 00:00:00 2001 From: dd84ai Date: Thu, 2 Nov 2023 02:56:52 +0100 Subject: [PATCH] refactor: improving returned answers to user --- app/configurator/tags.go | 10 ++++++++-- app/consoler/commands/alerts.go | 18 +++++++++++------- app/consoler/commands/tags.go | 12 ++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/configurator/tags.go b/app/configurator/tags.go index 618a8db..a297309 100644 --- a/app/configurator/tags.go +++ b/app/configurator/tags.go @@ -93,8 +93,14 @@ func (c ConfiguratorTags[T]) TagsList(channelID types.DiscordChannelID) ([]types result := c.db.Where("channel_id = ?", channelID).Find(&objs) logus.CheckWarn(result.Error, "unsuccesful result of c.db.Find") - return utils.CompL(objs, - func(x T) types.Tag { return x.GetTag() }), result.Error + tags := utils.CompL(objs, + func(x T) types.Tag { return x.GetTag() }) + + if result.RowsAffected == 0 { + return tags, ErrorZeroAffectedRows{} + } + + return tags, result.Error } func (c ConfiguratorTags[T]) TagsClear(channelID types.DiscordChannelID) error { diff --git a/app/consoler/commands/alerts.go b/app/consoler/commands/alerts.go index 8ea8dfe..20eb052 100644 --- a/app/consoler/commands/alerts.go +++ b/app/consoler/commands/alerts.go @@ -62,7 +62,11 @@ func (t *alertThresholdCommands[T]) CreateUnsetCmd() { logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args)) err := t.cfgTags.Unset(t.GetChannelID()) if err != nil { - printer.Println(cmd, "ERR msg="+err.Error()) + if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { + printer.Println(cmd, "ERR it is already unset") + } else { + printer.Println(cmd, "ERR ="+err.Error()) + } return } printer.Println(cmd, "OK Alert is unset") @@ -82,7 +86,7 @@ func (t *alertThresholdCommands[T]) CreateStatusCmd() { errMsg := err.Error() if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "OK status of alert is disabled") + printer.Println(cmd, "OK state is disabled") return } else { printer.Println(cmd, "ERR ="+errMsg) @@ -121,7 +125,7 @@ func (t *AlertBoolCommands[T]) CreateEnableCmd() { err := t.cfgTags.Enable(t.GetChannelID()) if err != nil { if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "ERR it was already enabled") + printer.Println(cmd, "ERR state was already enabled") } else { printer.Println(cmd, "ERR ="+err.Error()) } @@ -144,7 +148,7 @@ func (t *AlertBoolCommands[T]) CreateDisableCmd() { err := t.cfgTags.Disable(t.GetChannelID()) if err != nil { if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "ERR it was already disabled") + printer.Println(cmd, "ERR state was already disabled") } else { printer.Println(cmd, "ERR ="+err.Error()) } @@ -167,7 +171,7 @@ func (t *AlertBoolCommands[T]) CreateStatusCmd() { errMsg := err.Error() if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "OK status is disabled") + printer.Println(cmd, "OK alert status is disabled") return } else { printer.Println(cmd, "ERR ="+errMsg) @@ -229,7 +233,7 @@ func (t *AlertSetStringCommand[T]) CreateUnsetCmd() { err := t.cfgTags.Unset(t.GetChannelID()) if err != nil { if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "ERR it was already unset") + printer.Println(cmd, "ERR state was already unset") } else { printer.Println(cmd, "ERR ="+err.Error()) } @@ -251,7 +255,7 @@ func (t *AlertSetStringCommand[T]) CreateStatusCmd() { if err != nil { errMsg := err.Error() if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { - printer.Println(cmd, "OK status of alert is disabled") + printer.Println(cmd, "OK state of alert is disabled") return } else { printer.Println(cmd, "ERR ="+errMsg) diff --git a/app/consoler/commands/tags.go b/app/consoler/commands/tags.go index 7a33313..9155ba1 100644 --- a/app/consoler/commands/tags.go +++ b/app/consoler/commands/tags.go @@ -79,7 +79,11 @@ func (t *tagCommands) CreateTagClear() { logus.Debug("CreateTagClear.consoler running with args=", logus.Args(args)) err := t.cfgTags.TagsClear(t.GetChannelID()) if err != nil { - printer.Println(cmd, "ERR msg="+err.Error()) + if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { + printer.Println(cmd, "ERR list is already empty. nothing to clear.") + } else { + printer.Println(cmd, "ERR ="+err.Error()) + } return } @@ -98,7 +102,11 @@ func (t *tagCommands) CreateTagList() { tags, cfgErr := t.cfgTags.TagsList(t.GetChannelID()) err := cfgErr if err != nil { - printer.Println(cmd, "ERR msg="+err.Error()) + if _, ok := err.(configurator.ErrorZeroAffectedRows); ok { + printer.Println(cmd, "OK tag list is empty") + } else { + printer.Println(cmd, "ERR ="+err.Error()) + } return }