Skip to content

Commit 4f06dbf

Browse files
committed
feat: Add detailed validation error messages to ApiComparison
- Add InvalidMessage property to ApiComparison class for detailed error reporting - Enhanced IsValid() method to provide specific validation failure details - Updated ExitCodeManager to include InvalidMessage in log output - Improves debugging experience when API comparisons fail validation Resolves #51
1 parent 1609821 commit 4f06dbf

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/DotNetApiDiff/ExitCodes/ExitCodeManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ public int GetExitCode(ApiComparison apiComparison)
134134
// Validate the comparison result
135135
if (!apiComparison.IsValid())
136136
{
137-
_logger?.LogWarning("API comparison is invalid, returning ComparisonError exit code");
137+
var errorMessage = string.IsNullOrEmpty(apiComparison.InvalidMessage)
138+
? "API comparison is invalid"
139+
: $"API comparison is invalid: {apiComparison.InvalidMessage}";
140+
_logger?.LogWarning("{ErrorMessage}, returning ComparisonError exit code", errorMessage);
138141
return ComparisonError;
139142
}
140143

src/DotNetApiDiff/Models/ApiComparison.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class ApiComparison
3131
/// </summary>
3232
public ComparisonSummary Summary { get; set; } = new ComparisonSummary();
3333

34+
/// <summary>
35+
/// Gets the validation error message if the comparison is invalid, empty if valid
36+
/// </summary>
37+
public string InvalidMessage { get; private set; } = string.Empty;
38+
3439
/// <summary>
3540
/// Gets all changes as a single collection
3641
/// </summary>
@@ -63,34 +68,47 @@ public IEnumerable<ApiChange> AllChanges
6368
/// <returns>True if valid, false otherwise</returns>
6469
public bool IsValid()
6570
{
71+
// Clear any previous validation message
72+
InvalidMessage = string.Empty;
73+
6674
// Validate all changes
6775
var allChanges = AllChanges.ToList();
68-
if (allChanges.Any(c => !c.IsValid()))
76+
var invalidChanges = allChanges.Where(c => !c.IsValid()).ToList();
77+
if (invalidChanges.Any())
6978
{
79+
InvalidMessage = $"Found {invalidChanges.Count} invalid change(s): {string.Join(", ", invalidChanges.Select(c => $"'{c.Description}' ({c.Type})"))}";
7080
return false;
7181
}
7282

7383
// Validate that additions only contain Added changes
74-
if (Additions.Any(c => c.Type != ChangeType.Added))
84+
var wrongAdditions = Additions.Where(c => c.Type != ChangeType.Added).ToList();
85+
if (wrongAdditions.Any())
7586
{
87+
InvalidMessage = $"Additions collection contains {wrongAdditions.Count} change(s) with wrong type: {string.Join(", ", wrongAdditions.Select(c => $"'{c.Description}' ({c.Type})"))}";
7688
return false;
7789
}
7890

7991
// Validate that removals only contain Removed changes
80-
if (Removals.Any(c => c.Type != ChangeType.Removed))
92+
var wrongRemovals = Removals.Where(c => c.Type != ChangeType.Removed).ToList();
93+
if (wrongRemovals.Any())
8194
{
95+
InvalidMessage = $"Removals collection contains {wrongRemovals.Count} change(s) with wrong type: {string.Join(", ", wrongRemovals.Select(c => $"'{c.Description}' ({c.Type})"))}";
8296
return false;
8397
}
8498

8599
// Validate that modifications only contain Modified changes
86-
if (Modifications.Any(c => c.Type != ChangeType.Modified))
100+
var wrongModifications = Modifications.Where(c => c.Type != ChangeType.Modified).ToList();
101+
if (wrongModifications.Any())
87102
{
103+
InvalidMessage = $"Modifications collection contains {wrongModifications.Count} change(s) with wrong type: {string.Join(", ", wrongModifications.Select(c => $"'{c.Description}' ({c.Type})"))}";
88104
return false;
89105
}
90106

91107
// Validate that excluded only contain Excluded changes
92-
if (Excluded.Any(c => c.Type != ChangeType.Excluded))
108+
var wrongExcluded = Excluded.Where(c => c.Type != ChangeType.Excluded).ToList();
109+
if (wrongExcluded.Any())
93110
{
111+
InvalidMessage = $"Excluded collection contains {wrongExcluded.Count} change(s) with wrong type: {string.Join(", ", wrongExcluded.Select(c => $"'{c.Description}' ({c.Type})"))}";
94112
return false;
95113
}
96114

0 commit comments

Comments
 (0)