Skip to content

Commit

Permalink
refactor: improving returned answers to user
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 2, 2023
1 parent fcce601 commit 46dbfb0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 8 additions & 2 deletions app/configurator/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 11 additions & 7 deletions app/consoler/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions app/consoler/commands/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 46dbfb0

Please sign in to comment.