Skip to content

Commit a067c80

Browse files
authored
Improve error handling for duplicate display names (#3091)
Wrap SingleOrDefault by display name in try-catch to handle cases where multiple target users share the same display name. Log detailed error and warning messages, including a suggestion to enable MatchUsersByEmail and a list of matching users, before rethrowing the exception for better diagnostics. Fix for #3090
2 parents df93ad5 + 4e572bb commit a067c80

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/MigrationTools.Clients.TfsObjectModel/Tools/TfsUserMappingTool.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,21 @@ public IdentityMapResult GetUsersInSourceMappedToTarget(TfsProcessor processor)
190190
targetUser = candidates[0];
191191
}
192192
}
193-
targetUser ??= targetUsers.SingleOrDefault(x => x.DisplayName == sourceUser.DisplayName);
193+
try
194+
{
195+
targetUser ??= targetUsers.SingleOrDefault(x => x.DisplayName == sourceUser.DisplayName);
196+
}
197+
catch (InvalidOperationException)
198+
{
199+
Log.LogError("TfsUserMappingTool::GetUsersInSourceMappedToTarget:: Multiple target users found with the same display name '{displayName}'. "
200+
+ "Consider enabling MatchUsersByEmail option to avoid this issue. ",
201+
sourceUser.DisplayName, sourceUser.AccountName);
202+
var matchingUsers = targetUsers.Where(x => x.DisplayName == sourceUser.DisplayName).ToList();
203+
Log.LogWarning("TfsUserMappingTool::GetUsersInSourceMappedToTarget:: The list of matching users is {matchingUsers}",
204+
string.Join(", ", matchingUsers.Select(x => $"{x.DisplayName} [{x.MailAddress}]")));
205+
throw;
206+
}
207+
194208
identityMap.Add(new IdentityMapData { Source = sourceUser, Target = targetUser });
195209
}
196210
return new()

0 commit comments

Comments
 (0)