1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-01-03 22:52:23 +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 {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
_, err := d.client.ChannelMessageSend(channelID, fullMessage)
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
// html as markup language.
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{
To: m.receiverAddresses,
From: m.senderAddress,
@ -59,9 +52,15 @@ func (m Mail) Send(ctx context.Context, subject, message string) error {
Headers: textproto.MIMEHeader{},
}
err := msg.Send(m.smtpHostAddr, m.smtpAuth)
if err != nil {
err = errors.Wrap(err, "failed to send mail")
var err error
select {
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

View File

@ -53,7 +53,7 @@ func (m MSTeams) Send(ctx context.Context, subject, message string) error {
for _, webHook := range m.webHooks {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
err := m.client.SendWithContext(ctx, webHook, msgCard)
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.
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
var dst string
@ -98,13 +91,19 @@ func (s *Service) Send(ctx context.Context, subject, message string) error {
dst = strings.Join(s.destinations, "<")
}
_, err := s.client.Create(plivo.MessageCreateParams{
Dst: dst,
Text: text,
Src: s.mopts.Source,
URL: s.mopts.CallbackURL,
Method: s.mopts.CallbackMethod,
})
var err error
select {
case <-ctx.Done():
return ctx.Err()
default:
_, err = s.client.Create(plivo.MessageCreateParams{
Dst: dst,
Text: text,
Src: s.mopts.Source,
URL: s.mopts.CallbackURL,
Method: s.mopts.CallbackMethod,
})
}
return err
}

View File

@ -41,7 +41,7 @@ func (pb Pushbullet) Send(ctx context.Context, subject, message string) error {
for _, deviceNickname := range pb.deviceNicknames {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
dev, err := pb.client.Device(deviceNickname)
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 {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
err = sms.client.PushSMS(user.Iden, sms.deviceIdentifier, phoneNumber, fullMessage)
if err != nil {

View File

@ -54,21 +54,20 @@ func (s SendGrid) Send(ctx context.Context, subject, message string) error {
mailMessage.AddContent(content)
mailMessage.SetFrom(from)
// TODO: Should we check here or at the beginning of the func?
var err error
select {
case <-ctx.Done():
return nil
return ctx.Err()
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)
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
return err
}

View File

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

View File

@ -49,7 +49,7 @@ func (t Telegram) Send(ctx context.Context, subject, message string) error {
for _, chatID := range t.chatIDs {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
msg.ChatID = chatID
_, 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 {
select {
case <-ctx.Done():
return nil
return ctx.Err()
default:
directMessageTarget := &twitter.DirectMessageTarget{
RecipientID: twitterID,

View File

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