Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit b5ff9b3

Browse files
committed
Add error handling for Auth
1 parent 67c214a commit b5ff9b3

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

auth.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
)
1010

1111
// Auth handles simple password authentication of a user
12-
func (c *Client) Auth(input *tgbotapi.Message) {
12+
func (c *Client) Auth(input *tgbotapi.Message) error {
1313
msg := tgbotapi.NewMessage(input.Chat.ID, "")
1414
msg.ReplyToMessageID = input.MessageID
1515
if input.Text == c.Password {
1616
err := c.registerUser(input.From)
1717
if err != nil {
18-
log.Fatal(err)
18+
return err
1919
}
2020

2121
log.Printf("Allowed users: %s", strings.Join(c.listUsers(), ","))
@@ -24,8 +24,10 @@ func (c *Client) Auth(input *tgbotapi.Message) {
2424
msg.Text = "Wrong password"
2525
}
2626
if _, err := c.Bot.Send(msg); err != nil {
27-
log.Println(err)
27+
return err
2828
}
29+
30+
return nil
2931
}
3032

3133
// CheckAuth checks if user is allowed to interact with a bot

run.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"github.com/go-telegram-bot-api/telegram-bot-api"
88
)
99

10-
const defaultTimeout = 20
10+
const defaultPollTimeout = 20
1111

1212
// Run represents infinite function that waits for a message,
1313
// authenticate user and process task
1414
func (c *Client) Run() {
1515
defer c.DB.Close()
1616

1717
u := tgbotapi.NewUpdate(0)
18-
u.Timeout = defaultTimeout
18+
u.Timeout = defaultPollTimeout
1919

2020
updates, err := c.Bot.GetUpdatesChan(u)
2121
if err != nil {
@@ -59,7 +59,9 @@ func (c *Client) Run() {
5959
}
6060
}()
6161
} else {
62-
c.Auth(update.Message)
62+
if err := c.Auth(update.Message); err != nil {
63+
log.Println(err)
64+
}
6365
}
6466
}
6567
}

0 commit comments

Comments
 (0)