Skip to content

Commit

Permalink
fix: improve error handling and logging
Browse files Browse the repository at this point in the history
fix(utils.go): replace panic with logrus error logging and return false
fix(webhook.go): add logrus error logging when media download fails
fix(webhook.go): add logrus info and warning logging for webhook submission attempts
  • Loading branch information
aldinokemal committed Dec 19, 2024
1 parent 0b59619 commit c3bd17f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pkg/whatsapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func IsOnWhatsapp(waCli *whatsmeow.Client, jid string) bool {
if strings.Contains(jid, "@s.whatsapp.net") {
data, err := waCli.IsOnWhatsApp([]string{jid})
if err != nil {
panic(pkgError.InvalidJID(err.Error()))
logrus.Error("Failed to check if user is on whatsapp: ", err)
return false
}

for _, v := range data {
Expand Down
7 changes: 7 additions & 0 deletions src/pkg/whatsapp/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,39 @@ func createPayload(evt *events.Message) (map[string]interface{}, error) {
if imageMedia != nil {
path, err := ExtractMedia(config.PathMedia, imageMedia)
if err != nil {
logrus.Errorf("Failed to download image from %s: %v", evt.Info.SourceString(), err)
return nil, pkgError.WebhookError(fmt.Sprintf("Failed to download image: %v", err))
}
body["image"] = path
}
if stickerMedia != nil {
path, err := ExtractMedia(config.PathMedia, stickerMedia)
if err != nil {
logrus.Errorf("Failed to download sticker from %s: %v", evt.Info.SourceString(), err)
return nil, pkgError.WebhookError(fmt.Sprintf("Failed to download sticker: %v", err))
}
body["sticker"] = path
}
if videoMedia != nil {
path, err := ExtractMedia(config.PathMedia, videoMedia)
if err != nil {
logrus.Errorf("Failed to download video from %s: %v", evt.Info.SourceString(), err)
return nil, pkgError.WebhookError(fmt.Sprintf("Failed to download video: %v", err))
}
body["video"] = path
}
if audioMedia != nil {
path, err := ExtractMedia(config.PathMedia, audioMedia)
if err != nil {
logrus.Errorf("Failed to download audio from %s: %v", evt.Info.SourceString(), err)
return nil, pkgError.WebhookError(fmt.Sprintf("Failed to download audio: %v", err))
}
body["audio"] = path
}
if documentMedia != nil {
path, err := ExtractMedia(config.PathMedia, documentMedia)
if err != nil {
logrus.Errorf("Failed to download document from %s: %v", evt.Info.SourceString(), err)
return nil, pkgError.WebhookError(fmt.Sprintf("Failed to download document: %v", err))
}
body["document"] = path
Expand Down Expand Up @@ -127,8 +132,10 @@ func submitWebhook(payload map[string]interface{}) error {

for attempt = 0; attempt < maxAttempts; attempt++ {
if _, err = client.Do(req); err == nil {
logrus.Infof("Successfully submitted webhook on attempt %d", attempt+1)
return nil
}
logrus.Warnf("Attempt %d to submit webhook failed: %v", attempt+1, err)
time.Sleep(sleepDuration)
sleepDuration *= 2
}
Expand Down

0 comments on commit c3bd17f

Please sign in to comment.