Handle document mapping conflict error

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-03 10:39:30 +02:00
parent 6210ce17f3
commit 9fdf927c9b
2 changed files with 31 additions and 1 deletions

View File

@@ -16,12 +16,14 @@ package coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
)
@@ -34,8 +36,17 @@ type (
}
ControlDocuments []*ControlDocument
ErrControlDocumentMappingAlreadyExists struct {
ControlID gid.GID
DocumentID gid.GID
}
)
func (e ErrControlDocumentMappingAlreadyExists) Error() string {
return fmt.Sprintf("control %s is already mapped to document %s", e.ControlID, e.DocumentID)
}
func (cp ControlDocument) Insert(
ctx context.Context,
conn pg.Conn,
@@ -64,7 +75,22 @@ VALUES (
"created_at": cp.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" && pgErr.ConstraintName == "controls_policies_pkey" {
return &ErrControlDocumentMappingAlreadyExists{
ControlID: cp.ControlID,
DocumentID: cp.DocumentID,
}
}
}
return fmt.Errorf("cannot insert control document: %w", err)
}
return nil
}
func (cp ControlDocument) Delete(

View File

@@ -1878,6 +1878,10 @@ func (r *mutationResolver) CreateControlDocumentMapping(ctx context.Context, inp
control, document, err := prb.Controls.CreateDocumentMapping(ctx, input.ControlID, input.DocumentID)
if err != nil {
var errMappingExists *coredata.ErrControlDocumentMappingAlreadyExists
if errors.As(err, &errMappingExists) {
return nil, errors.New(errMappingExists.Error())
}
panic(fmt.Errorf("cannot create control document mapping: %w", err))
}