Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Suppress listmonk missing subscriber alerts

Manav Rathidb03f628c973
diff --git a/server/pkg/controller/mailing_lists.go b/server/pkg/controller/mailing_lists.go
index 6ae3b0d69a..be2be74f3f 100644
--- a/server/pkg/controller/mailing_lists.go
+++ b/server/pkg/controller/mailing_lists.go
@@ -1,6 +1,7 @@
package controller
import (
+ "errors"
"fmt"
"net/url"
"strconv"
@@ -234,8 +235,10 @@ func (c *MailingListsController) listmonkUnsubscribe(email string) error {
id, err := listmonk.GetSubscriberID(c.listmonkCredentials.BaseURL+"/api/subscribers",
c.listmonkCredentials.Username, c.listmonkCredentials.Password, email)
if err != nil {
- log.Errorf("Listmonk - Unsub failed, could not find subscriber '%s': %s", email, err)
- c.notifyListmonkFailure("Listmonk - Unsub failed, could not find subscriber", err)
+ log.Errorf("Listmonk - Unsub failed for '%s': %s", email, err)
+ if !errors.Is(err, listmonk.ErrSubscriberNotFound) {
+ c.notifyListmonkFailure("Listmonk - Unsub failed", err)
+ }
return stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/controller/mailing_lists_test.go b/server/pkg/controller/mailing_lists_test.go
new file mode 100644
index 0000000000..20a1a6e241
--- /dev/null
+++ b/server/pkg/controller/mailing_lists_test.go
@@ -0,0 +1,30 @@
+package controller
+
+import (
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/ente-io/museum/pkg/external/listmonk"
+)
+
+func TestListmonkUnsubscribeDoesNotNotifyWhenSubscriberIsMissing(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = w.Write([]byte(`{"data":{"results":[]}}`))
+ }))
+ defer server.Close()
+
+ c := &MailingListsController{
+ listmonkCredentials: listmonk.Credentials{
+ BaseURL: server.URL,
+ Username: "user",
+ Password: "pass",
+ },
+ }
+
+ err := c.listmonkUnsubscribe("[email protected]")
+ if !errors.Is(err, listmonk.ErrSubscriberNotFound) {
+ t.Fatalf("err = %v, want ErrSubscriberNotFound", err)
+ }
+}
diff --git a/server/pkg/external/listmonk/api.go b/server/pkg/external/listmonk/api.go
index 2ad80e13df..bafadbe6c1 100644
--- a/server/pkg/external/listmonk/api.go
+++ b/server/pkg/external/listmonk/api.go
@@ -28,6 +28,8 @@ type Credentials struct {
const listmonkRequestTimeout = 60 * time.Second
+var ErrSubscriberNotFound = errors.New("subscriber not found")
+
// Subscriber captures a listmonk subscriber record required by museum.
type Subscriber struct {
Email string
@@ -104,7 +106,7 @@ func GetSubscriberID(endpoint string, username string, password string, subscrib
// Checking if there are any subscribers found
if len(subscriberResp.Data.Results) == 0 {
- return 0, stacktrace.Propagate(errors.New("subscriber not found"), "")
+ return 0, stacktrace.Propagate(ErrSubscriberNotFound, "")
}
// Extracting the ID from the response
diff --git a/server/pkg/external/listmonk/api_test.go b/server/pkg/external/listmonk/api_test.go
index a613b62c12..3b9d4a8a10 100644
--- a/server/pkg/external/listmonk/api_test.go
+++ b/server/pkg/external/listmonk/api_test.go
@@ -1,6 +1,7 @@
package listmonk
import (
+ "errors"
"net/http"
"net/http/httptest"
"strings"
@@ -18,6 +19,9 @@ func TestGetSubscriberIDReturnsStatusError(t *testing.T) {
if err == nil {
t.Fatal("expected error")
}
+ if errors.Is(err, ErrSubscriberNotFound) {
+ t.Fatal("status failure was reported as subscriber not found")
+ }
if !strings.Contains(err.Error(), "status 401") {
t.Fatalf("err = %v, want status 401", err)
}