Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add human friendly error messages #1154

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/bot/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -269,7 +270,7 @@ func (b *Discord) send(channelID string, resp interactive.CoreMessage) error {
return fmt.Errorf("while formatting message: %w", err)
}
if _, err := b.api.ChannelMessageSendComplex(channelID, discordMsg); err != nil {
return fmt.Errorf("while sending message: %w", err)
return fmt.Errorf("while sending message: %w", discordError(err, channelID))
}

b.log.Debugf("Message successfully sent to channel %q", channelID)
Expand Down Expand Up @@ -355,7 +356,7 @@ func discordChannelsConfigFrom(log logrus.FieldLogger, api *discordgo.Session, c

channelData, err := api.Channel(channCfg.Identifier())
if err != nil {
return nil, fmt.Errorf("while getting channel name for ID %q: %w", channCfg.Identifier(), err)
return nil, fmt.Errorf("while getting channel name for ID %q: %w", channCfg.Identifier(), discordError(err, channCfg.Identifier()))
}

res[channCfg.Identifier()] = channelConfigByID{
Expand All @@ -377,3 +378,16 @@ func discordBotMentionRegex(botID string) (*regexp.Regexp, error) {

return botMentionRegex, nil
}

func discordError(err error, channel string) error {
switch err := err.(type) {
case *discordgo.RESTError:
switch err.Response.StatusCode {
case http.StatusUnauthorized:
return errors.New("invalid discord credentials")
case http.StatusNotFound:
return fmt.Errorf("channel %q not found", channel)
}
}
return err
}
12 changes: 12 additions & 0 deletions pkg/bot/slack_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func slackBotMentionRegex(botID string) (*regexp.Regexp, error) {
return botMentionRegex, nil
}

func slackError(err error, channel string) error {
switch err.Error() {
case "channel_not_found":
err = fmt.Errorf("channel %q not found", channel)
case "not_in_channel":
err = fmt.Errorf("botkube is not in channel %q", channel)
case "invalid_auth":
err = fmt.Errorf("invalid slack credentials")
}
return err
}

// slackMessage contains message details to execute command and send back the result
type slackMessage struct {
Text string
Expand Down
4 changes: 2 additions & 2 deletions pkg/bot/slack_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewSocketSlack(log logrus.FieldLogger, commGroupName string, cfg config.Soc

authResp, err := client.AuthTest()
if err != nil {
return nil, fmt.Errorf("while testing the ability to do auth Slack request: %w", err)
return nil, fmt.Errorf("while testing the ability to do auth Slack request: %w", slackError(err, ""))
}
botID := authResp.UserID

Expand Down Expand Up @@ -422,7 +422,7 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, resp interac
}
} else {
if _, _, err := b.client.PostMessageContext(ctx, event.Channel, options...); err != nil {
return fmt.Errorf("while posting Slack message: %w", err)
return fmt.Errorf("while posting Slack message: %w", slackError(err, event.Channel))
}
}

Expand Down
Loading