Skip to content

The only tool you need to know what is happening and how to fix it.

fix: capturing the exception when sending emails from a separate go routine

ddux7aceb2baf320
diff --git a/backend/app/controllers/invitation.controller.go b/backend/app/controllers/invitation.controller.go
index 6b99c34b..77b13d6b 100644
--- a/backend/app/controllers/invitation.controller.go
+++ b/backend/app/controllers/invitation.controller.go
@@ -7,6 +7,7 @@ import (
"github.com/tracewayapp/traceway/backend/app/repositories"
"github.com/tracewayapp/traceway/backend/app/services"
"database/sql"
+ "fmt"
"net/http"
"strconv"
"time"
@@ -88,7 +89,11 @@ func (c *invitationController) InviteUser(ctx *gin.Context) {
return
}
- go services.EmailService.SendInvitation(request.Email, inviter.Name, org.Name, invitation.Token)
+ go func() {
+ if err := services.EmailService.SendInvitation(request.Email, inviter.Name, org.Name, invitation.Token); err != nil {
+ traceway.CaptureException(fmt.Errorf("failed to send invitation email to %s: %w", request.Email, err))
+ }
+ }()
ctx.JSON(http.StatusCreated, gin.H{"message": "Invitation sent"})
}
diff --git a/backend/app/controllers/password_reset.controller.go b/backend/app/controllers/password_reset.controller.go
index 31ac8177..7d753595 100644
--- a/backend/app/controllers/password_reset.controller.go
+++ b/backend/app/controllers/password_reset.controller.go
@@ -6,6 +6,7 @@ import (
"github.com/tracewayapp/traceway/backend/app/repositories"
"github.com/tracewayapp/traceway/backend/app/services"
"database/sql"
+ "fmt"
"net/http"
"time"
@@ -53,7 +54,11 @@ func (c *passwordResetController) ForgotPassword(ctx *gin.Context) {
return
}
- go services.EmailService.SendPasswordReset(user.Email, token)
+ go func() {
+ if err := services.EmailService.SendPasswordReset(user.Email, token); err != nil {
+ traceway.CaptureException(fmt.Errorf("failed to send password reset email to %s: %w", user.Email, err))
+ }
+ }()
ctx.JSON(http.StatusOK, gin.H{"message": "If an account exists with this email, a password reset link will be sent to it."})
}