1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-01-05 22:53:35 +02:00

refactor(service): return context.Err when context.IsDone

This commit is contained in:
Niko Köser 2021-02-22 23:56:28 +01:00
parent 80cab3dece
commit 19d5488113
No known key found for this signature in database
GPG Key ID: F3F28C118DAA6375
11 changed files with 41 additions and 44 deletions

View File

@ -96,7 +96,7 @@ func (d Discord) Send(ctx context.Context, subject, message string) error {
for _, channelID := range d.channelIDs { for _, channelID := range d.channelIDs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
_, err := d.client.ChannelMessageSend(channelID, fullMessage) _, err := d.client.ChannelMessageSend(channelID, fullMessage)
if err != nil { if err != nil {

View File

@ -43,13 +43,6 @@ func (m *Mail) AddReceivers(addresses ...string) {
// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports // Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
// html as markup language. // html as markup language.
func (m Mail) Send(ctx context.Context, subject, message string) error { func (m Mail) Send(ctx context.Context, subject, message string) error {
// TODO: Is this necessary? Or do we just do nothing with ctx?
select {
case <-ctx.Done():
return nil
default:
}
msg := &email.Email{ msg := &email.Email{
To: m.receiverAddresses, To: m.receiverAddresses,
From: m.senderAddress, From: m.senderAddress,
@ -59,9 +52,15 @@ func (m Mail) Send(ctx context.Context, subject, message string) error {
Headers: textproto.MIMEHeader{}, Headers: textproto.MIMEHeader{},
} }
err := msg.Send(m.smtpHostAddr, m.smtpAuth) var err error
if err != nil { select {
err = errors.Wrap(err, "failed to send mail") case <-ctx.Done():
err = ctx.Err()
default:
err = msg.Send(m.smtpHostAddr, m.smtpAuth)
if err != nil {
err = errors.Wrap(err, "failed to send mail")
}
} }
return err return err

View File

@ -53,7 +53,7 @@ func (m MSTeams) Send(ctx context.Context, subject, message string) error {
for _, webHook := range m.webHooks { for _, webHook := range m.webHooks {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
err := m.client.SendWithContext(ctx, webHook, msgCard) err := m.client.SendWithContext(ctx, webHook, msgCard)
if err != nil { if err != nil {

View File

@ -77,13 +77,6 @@ func (s *Service) AddReceivers(phoneNumbers ...string) {
// Send sends a SMS via Plivo to all previously added receivers. // Send sends a SMS via Plivo to all previously added receivers.
func (s *Service) Send(ctx context.Context, subject, message string) error { func (s *Service) Send(ctx context.Context, subject, message string) error {
// TODO: Same question here.
select {
case <-ctx.Done():
return nil
default:
}
text := subject + "\n" + message text := subject + "\n" + message
var dst string var dst string
@ -98,13 +91,19 @@ func (s *Service) Send(ctx context.Context, subject, message string) error {
dst = strings.Join(s.destinations, "<") dst = strings.Join(s.destinations, "<")
} }
_, err := s.client.Create(plivo.MessageCreateParams{ var err error
Dst: dst, select {
Text: text, case <-ctx.Done():
Src: s.mopts.Source, return ctx.Err()
URL: s.mopts.CallbackURL, default:
Method: s.mopts.CallbackMethod, _, err = s.client.Create(plivo.MessageCreateParams{
}) Dst: dst,
Text: text,
Src: s.mopts.Source,
URL: s.mopts.CallbackURL,
Method: s.mopts.CallbackMethod,
})
}
return err return err
} }

View File

@ -41,7 +41,7 @@ func (pb Pushbullet) Send(ctx context.Context, subject, message string) error {
for _, deviceNickname := range pb.deviceNicknames { for _, deviceNickname := range pb.deviceNicknames {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
dev, err := pb.client.Device(deviceNickname) dev, err := pb.client.Device(deviceNickname)
if err != nil { if err != nil {

View File

@ -55,7 +55,7 @@ func (sms SMS) Send(ctx context.Context, subject, message string) error {
for _, phoneNumber := range sms.phoneNumbers { for _, phoneNumber := range sms.phoneNumbers {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
err = sms.client.PushSMS(user.Iden, sms.deviceIdentifier, phoneNumber, fullMessage) err = sms.client.PushSMS(user.Iden, sms.deviceIdentifier, phoneNumber, fullMessage)
if err != nil { if err != nil {

View File

@ -54,21 +54,20 @@ func (s SendGrid) Send(ctx context.Context, subject, message string) error {
mailMessage.AddContent(content) mailMessage.AddContent(content)
mailMessage.SetFrom(from) mailMessage.SetFrom(from)
// TODO: Should we check here or at the beginning of the func? var err error
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
resp, err := s.client.Send(mailMessage)
if err != nil {
err = errors.Wrap(err, "failed to send mail using SendGrid service")
}
if resp.StatusCode != http.StatusAccepted {
err = errors.New("the SendGrid endpoint did not accept the message")
}
} }
resp, err := s.client.Send(mailMessage) return err
if err != nil {
return errors.Wrap(err, "failed to send mail using SendGrid service")
}
if resp.StatusCode != http.StatusAccepted {
return errors.New("the SendGrid endpoint did not accept the message")
}
return nil
} }

View File

@ -42,7 +42,7 @@ func (s Slack) Send(ctx context.Context, subject, message string) error {
for _, channelID := range s.channelIDs { for _, channelID := range s.channelIDs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
id, timestamp, err := s.client.PostMessageContext( id, timestamp, err := s.client.PostMessageContext(
ctx, ctx,

View File

@ -49,7 +49,7 @@ func (t Telegram) Send(ctx context.Context, subject, message string) error {
for _, chatID := range t.chatIDs { for _, chatID := range t.chatIDs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
msg.ChatID = chatID msg.ChatID = chatID
_, err := t.client.Send(msg) _, err := t.client.Send(msg)

View File

@ -78,7 +78,7 @@ func (t Twitter) Send(ctx context.Context, subject, message string) error {
for _, twitterID := range t.twitterIDs { for _, twitterID := range t.twitterIDs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
directMessageTarget := &twitter.DirectMessageTarget{ directMessageTarget := &twitter.DirectMessageTarget{
RecipientID: twitterID, RecipientID: twitterID,

View File

@ -173,7 +173,7 @@ func (s *Service) Send(ctx context.Context, subject, message string) error {
for _, contact := range s.contacts { for _, contact := range s.contacts {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return ctx.Err()
default: default:
msg.Info = whatsapp.MessageInfo{ msg.Info = whatsapp.MessageInfo{
RemoteJid: contact + "@s.whatsapp.net", RemoteJid: contact + "@s.whatsapp.net",