Skip to content

Commit 482f5e4

Browse files
committed
fix: double check the constraint error if save failed
1 parent 341def4 commit 482f5e4

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

openmeter/subscription/addon/repo/subscriptionaddon.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package subscriptionaddonrepo
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

8+
"github.com/jackc/pgx/v5/pgconn"
79
"github.com/openmeterio/openmeter/openmeter/ent/db"
810
dbsubscriptionaddon "github.com/openmeterio/openmeter/openmeter/ent/db/subscriptionaddon"
911
dbsubscriptionaddonquantity "github.com/openmeterio/openmeter/openmeter/ent/db/subscriptionaddonquantity"
@@ -45,9 +47,12 @@ func (r *subscriptionAddonRepo) Create(ctx context.Context, namespace string, in
4547
// Keep the user-visible message generic so it does not echo addon/subscription IDs;
4648
// the workflow's pre-Create validation already establishes that both exist.
4749
if db.IsConstraintError(err) {
48-
return nil, models.NewGenericConflictError(
49-
fmt.Errorf("addon is already attached to subscription"),
50-
)
50+
var pgErr *pgconn.PgError
51+
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
52+
return nil, models.NewGenericConflictError(
53+
fmt.Errorf("addon is already attached to subscription"),
54+
)
55+
}
5156
}
5257

5358
return nil, err

0 commit comments

Comments
 (0)