Skip to content

Commit

Permalink
Stop throwing "exists" error on public forms
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jan 31, 2021
1 parent 62bce69 commit dd0c124
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func handleSubscriptionForm(c echo.Context) error {
// Insert the subscriber into the DB.
req.Status = models.SubscriberStatusEnabled
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,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", err.(*echo.HTTPError).Message)))
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -67,6 +68,8 @@ var (
}

subQuerySortFields = []string{"email", "name", "created_at", "updated_at"}

errSubscriberExists = errors.New("subscriber already exists")
)

// handleGetSubscriber handles the retrieval of a single subscriber by ID.
Expand Down Expand Up @@ -272,6 +275,10 @@ func handleCreateSubscriber(c echo.Context) error {
// Insert the subscriber into the DB.
sub, err := insertSubscriber(req, app)
if err != nil {
if err == errSubscriberExists {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.emailExists"))
}

return err
}

Expand Down Expand Up @@ -630,8 +637,7 @@ func insertSubscriber(req subimporter.SubReq, app *App) (models.Subscriber, erro
req.ListUUIDs)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" {
return req.Subscriber,
echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.emailExists"))
return req.Subscriber, errSubscriberExists
}

app.log.Printf("error inserting subscriber: %v", err)
Expand Down

0 comments on commit dd0c124

Please sign in to comment.