Skip to content

Commit 69a8270

Browse files
committed
Handle error in NewWebhook and NewWebhookWithCert
1 parent 2f7211a commit 69a8270

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

bot_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,13 @@ func TestSetWebhookWithCert(t *testing.T) {
479479

480480
bot.Request(RemoveWebhookConfig{})
481481

482-
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
483-
_, err := bot.Request(wh)
482+
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
483+
484+
if err != nil {
485+
t.Error(err)
486+
}
487+
_, err = bot.Request(wh)
488+
484489
if err != nil {
485490
t.Error(err)
486491
}
@@ -501,8 +506,14 @@ func TestSetWebhookWithoutCert(t *testing.T) {
501506

502507
bot.Request(RemoveWebhookConfig{})
503508

504-
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
505-
_, err := bot.Request(wh)
509+
wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
510+
511+
if err != nil {
512+
t.Error(err)
513+
}
514+
515+
_, err = bot.Request(wh)
516+
506517
if err != nil {
507518
t.Error(err)
508519
}
@@ -589,7 +600,14 @@ func ExampleNewWebhook() {
589600

590601
log.Printf("Authorized on account %s", bot.Self.UserName)
591602

592-
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
603+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
604+
605+
if err != nil {
606+
panic(err)
607+
}
608+
609+
_, err = bot.Request(wh)
610+
593611
if err != nil {
594612
panic(err)
595613
}
@@ -622,7 +640,13 @@ func ExampleWebhookHandler() {
622640

623641
log.Printf("Authorized on account %s", bot.Self.UserName)
624642

625-
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
643+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
644+
645+
if err != nil {
646+
panic(err)
647+
}
648+
649+
_, err = bot.Request(wh)
626650
if err != nil {
627651
panic(err)
628652
}

helpers.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,25 +441,33 @@ func NewUpdate(offset int) UpdateConfig {
441441
// NewWebhook creates a new webhook.
442442
//
443443
// link is the url parsable link you wish to get the updates.
444-
func NewWebhook(link string) WebhookConfig {
445-
u, _ := url.Parse(link)
444+
func NewWebhook(link string) (WebhookConfig, error) {
445+
u, err := url.Parse(link)
446+
447+
if err != nil {
448+
return WebhookConfig{}, err
449+
}
446450

447451
return WebhookConfig{
448452
URL: u,
449-
}
453+
}, nil
450454
}
451455

452456
// NewWebhookWithCert creates a new webhook with a certificate.
453457
//
454458
// link is the url you wish to get webhooks,
455459
// file contains a string to a file, FileReader, or FileBytes.
456-
func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
457-
u, _ := url.Parse(link)
460+
func NewWebhookWithCert(link string, file interface{}) (WebhookConfig, error) {
461+
u, err := url.Parse(link)
462+
463+
if err != nil {
464+
return WebhookConfig{}, err
465+
}
458466

459467
return WebhookConfig{
460468
URL: u,
461469
Certificate: file,
462-
}
470+
}, nil
463471
}
464472

465473
// NewInlineQueryResultArticle creates a new inline query article.

helpers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import (
44
"testing"
55
)
66

7+
func TestNewWebhook(t *testing.T) {
8+
result, err := NewWebhook("https://example.com/token")
9+
10+
if err != nil ||
11+
result.URL.String() != "https://example.com/token" ||
12+
result.Certificate != interface{}(nil) ||
13+
result.MaxConnections != 0 ||
14+
len(result.AllowedUpdates) != 0 {
15+
t.Fail()
16+
}
17+
}
18+
19+
func TestNewWebhookWithCert(t *testing.T) {
20+
exampleFile := File{FileID: "123"}
21+
result, err := NewWebhookWithCert("https://example.com/token", exampleFile)
22+
23+
if err != nil ||
24+
result.URL.String() != "https://example.com/token" ||
25+
result.Certificate != exampleFile ||
26+
result.MaxConnections != 0 ||
27+
len(result.AllowedUpdates) != 0 {
28+
t.Fail()
29+
}
30+
}
31+
732
func TestNewInlineQueryResultArticle(t *testing.T) {
833
result := NewInlineQueryResultArticle("id", "title", "message")
934

0 commit comments

Comments
 (0)