Fix 5xx when saml config already exist

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-12-24 11:59:42 +01:00
parent 61007559d7
commit 12c7c278ad
4 changed files with 27 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import (
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
@@ -317,6 +318,13 @@ INSERT INTO iam_saml_configurations (
_, err := conn.Exec(ctx, q, args)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_saml_config_domain_org_unique" {
return ErrResourceAlreadyExists
}
}
return fmt.Errorf("cannot insert saml_configuration: %w", err)
}

View File

@@ -308,6 +308,16 @@ func (e *ErrSAMLAuthenticationRequired) Error() string {
return fmt.Sprintf("SAML authentication required: %s", e.Reason)
}
type ErrSAMLConfigurationEmailDomainAlreadyExists struct{ EmailDomain string }
func NewSAMLConfigurationEmailDomainAlreadyExistsError(emailDomain string) error {
return &ErrSAMLConfigurationEmailDomainAlreadyExists{EmailDomain: emailDomain}
}
func (e ErrSAMLConfigurationEmailDomainAlreadyExists) Error() string {
return fmt.Sprintf("SAML configuration email domain %q already exists", e.EmailDomain)
}
// TenantAccessError is used by API recovery middleware to translate authorization/tenant failures
// into a consistent client-facing error response.
//

View File

@@ -1069,6 +1069,10 @@ func (s OrganizationService) CreateSAMLConfiguration(
err = config.Insert(ctx, tx, scope)
if err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return NewSAMLConfigurationEmailDomainAlreadyExistsError(req.EmailDomain)
}
return fmt.Errorf("cannot insert saml configuration: %w", err)
}

View File

@@ -915,6 +915,11 @@ func (r *mutationResolver) CreateSAMLConfiguration(ctx context.Context, input ty
)
if err != nil {
var errSAMLConfigurationEmailDomainAlreadyExists *iam.ErrSAMLConfigurationEmailDomainAlreadyExists
if errors.As(err, &errSAMLConfigurationEmailDomainAlreadyExists) {
return nil, gqlutils.Conflict(err)
}
r.logger.ErrorCtx(ctx, "cannot create saml configuration", log.Error(err))
return nil, gqlutils.InternalServerError(ctx)
}