Skip to content

Commit

Permalink
feat: improve slack error
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Nov 13, 2024
1 parent d30492f commit 17285db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion notification/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func SendNotification(ctx *Context, connectionName, shoutrrrURL string, celEnv m
}

if err := SlackSend(ctx, connection.Password, connection.Username, data); err != nil {
return "", ctx.Oops().Hint(data.Message).Wrap(err)
return "", err
}

return "slack", nil
Expand Down
16 changes: 15 additions & 1 deletion notification/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notification

import (
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -13,6 +14,10 @@ type SlackMsgTemplate struct {
}

func SlackSend(ctx *Context, apiToken, channel string, msg NotificationTemplate) error {
if channel == "" {
return errors.New("slack channel cannot be empty")
}

api := slack.New(apiToken)

var opts []slack.MsgOption
Expand All @@ -34,5 +39,14 @@ func SlackSend(ctx *Context, apiToken, channel string, msg NotificationTemplate)
}

_, _, err := api.PostMessage(channel, opts...)
return err

var slackError slack.SlackErrorResponse
if errors.As(err, &slackError) {
switch slackError.Err {
case "channel_not_found":
return fmt.Errorf("slack channel %q not found. ensure the channel exists & the bot has permission on that channel", channel)
}
}

return ctx.Oops().Hint(msg.Message).Wrap(err)
}

0 comments on commit 17285db

Please sign in to comment.