Return clear error on user delete FK violation
Deleting a membership profile referenced by other tables (owner, approver, assignee, etc.) surfaced as a generic Internal error. Detect the Postgres FK violation (23503) in the coredata Delete, return ErrResourceInUse, and map it to CONFLICT in the GraphQL and MCP resolvers so the client sees an actionable error. Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -1416,6 +1416,11 @@ WHERE
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
if pgErr.Code == "23503" {
|
||||
return ErrResourceInUse
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot delete profile: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -120,6 +120,10 @@ func (r *mutationResolver) RemoveUser(ctx context.Context, input types.RemoveUse
|
||||
return nil, gqlutils.Conflict(ctx, err)
|
||||
}
|
||||
|
||||
if errors.Is(err, coredata.ErrResourceInUse) {
|
||||
return nil, gqlutils.Conflict(ctx, err)
|
||||
}
|
||||
|
||||
r.logger.ErrorCtx(ctx, "cannot remove user from organization", log.Error(err))
|
||||
|
||||
return nil, gqlutils.Internal(ctx)
|
||||
|
||||
@@ -2574,6 +2574,9 @@ func (r *Resolver) RemoveUserTool(ctx context.Context, req *mcp.CallToolRequest,
|
||||
if _, ok := errors.AsType[*iam.ErrLastActiveOwner](err); ok {
|
||||
return nil, types.RemoveUserOutput{}, fmt.Errorf("cannot remove last active owner: %w", err)
|
||||
}
|
||||
if errors.Is(err, coredata.ErrResourceInUse) {
|
||||
return nil, types.RemoveUserOutput{}, fmt.Errorf("cannot remove user: %w", err)
|
||||
}
|
||||
|
||||
return nil, types.RemoveUserOutput{}, fmt.Errorf("remove user: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user