Stop throwing "exists" error on public forms

This commit is contained in:
Kailash Nadh 2021-01-31 13:38:02 +05:30
parent 62bce6902e
commit dd0c124b0e
2 changed files with 9 additions and 3 deletions

View File

@ -285,7 +285,7 @@ func handleSubscriptionForm(c echo.Context) error {
// Insert the subscriber into the DB. // Insert the subscriber into the DB.
req.Status = models.SubscriberStatusEnabled req.Status = models.SubscriberStatusEnabled
req.ListUUIDs = pq.StringArray(req.SubListUUIDs) req.ListUUIDs = pq.StringArray(req.SubListUUIDs)
if _, err := insertSubscriber(req.SubReq, app); err != nil { if _, err := insertSubscriber(req.SubReq, app); err != nil && err != errSubscriberExists {
return c.Render(http.StatusInternalServerError, tplMessage, return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", err.(*echo.HTTPError).Message))) makeMsgTpl(app.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", err.(*echo.HTTPError).Message)))
} }

View File

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -67,6 +68,8 @@ var (
} }
subQuerySortFields = []string{"email", "name", "created_at", "updated_at"} subQuerySortFields = []string{"email", "name", "created_at", "updated_at"}
errSubscriberExists = errors.New("subscriber already exists")
) )
// handleGetSubscriber handles the retrieval of a single subscriber by ID. // handleGetSubscriber handles the retrieval of a single subscriber by ID.
@ -272,6 +275,10 @@ func handleCreateSubscriber(c echo.Context) error {
// Insert the subscriber into the DB. // Insert the subscriber into the DB.
sub, err := insertSubscriber(req, app) sub, err := insertSubscriber(req, app)
if err != nil { if err != nil {
if err == errSubscriberExists {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.emailExists"))
}
return err return err
} }
@ -630,8 +637,7 @@ func insertSubscriber(req subimporter.SubReq, app *App) (models.Subscriber, erro
req.ListUUIDs) req.ListUUIDs)
if err != nil { if err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" { if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" {
return req.Subscriber, return req.Subscriber, errSubscriberExists
echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.emailExists"))
} }
app.log.Printf("error inserting subscriber: %v", err) app.log.Printf("error inserting subscriber: %v", err)