Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Handle error for new telegram client
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgreg committed Feb 27, 2024
1 parent 76afa80 commit a9250f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 8 additions & 1 deletion internal/alert/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
package alert

import (
"go.uber.org/zap"

"github.com/base-org/pessimism/internal/client"
"github.com/base-org/pessimism/internal/core"
"github.com/base-org/pessimism/internal/logging"
)

// RoutingDirectory ... Interface for routing directory
Expand Down Expand Up @@ -112,7 +115,11 @@ func (rd *routingDirectory) paramsToRouteDirectory(acc *core.AlertClientCfg, sev
ChatID: cfg.ChatID.String(),
Token: cfg.Token.String(),
}
client := client.NewTelegramClient(conf, name)
client, err := client.NewTelegramClient(conf, name)
if err != nil {
logging.NoContext().Error("Failed to create Telegram client", zap.String("name", name), zap.Error(err))
continue
}
rd.telegramClients[sev] = append(rd.telegramClients[sev], client)
}
}
Expand Down
8 changes: 5 additions & 3 deletions internal/client/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -28,17 +29,18 @@ type telegramClient struct {
client *http.Client
}

func NewTelegramClient(cfg *TelegramConfig, name string) TelegramClient {
func NewTelegramClient(cfg *TelegramConfig, name string) (TelegramClient, error) {
if cfg.Token == "" {
logging.NoContext().Warn("No Telegram token provided")
logging.NoContext().Warn("No Telegram bot token provided")
return nil, errors.New("No Telegram bot token was provided")
}

return &telegramClient{
token: cfg.Token,
chatID: cfg.ChatID,
name: name,
client: &http.Client{},
}
}, nil
}

type TelegramPayload struct {
Expand Down

0 comments on commit a9250f0

Please sign in to comment.