Skip to content

Commit 4145318

Browse files
committed
Added diagnostic for failure in ObservablePropertyGenerator
1 parent 25cf030 commit 4145318

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/AnalyzerReleases.Unshipped.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ MVVMTK0005 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableObjectGenerator |
1313
MVVMTK0006 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableObjectGenerator | Error | See https://aka.ms/mvvmtoolkit
1414
MVVMTK0007 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit
1515
MVVMTK0008 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit
16-
MVVMTK0009 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
16+
MVVMTK0009 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
17+
MVVMTK0010 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ public void Execute(GeneratorExecutionContext context)
4747
if (items.Key.DeclaringSyntaxReferences.Length > 0 &&
4848
items.Key.DeclaringSyntaxReferences.First().GetSyntax() is ClassDeclarationSyntax classDeclaration)
4949
{
50-
OnExecute(context, classDeclaration, items.Key, items);
50+
try
51+
{
52+
OnExecute(context, classDeclaration, items.Key, items);
53+
}
54+
catch
55+
{
56+
context.ReportDiagnostic(ObservablePropertyGeneratorError, items.Key, items.Key);
57+
}
5158
}
5259
}
5360
}

Microsoft.Toolkit.Mvvm.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,21 @@ internal static class DiagnosticDescriptors
156156
isEnabledByDefault: true,
157157
description: $"Cannot apply [{nameof(ObservablePropertyAttribute)}] to fields with validation attributes if they are declared in a type that doesn't inherit from ObservableValidator.",
158158
helpLinkUri: "https://aka.ms/mvvmtoolkit");
159+
160+
/// <summary>
161+
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when <see cref="ObservablePropertyGenerator"/> failed to run on a given type.
162+
/// <para>
163+
/// Format: <c>"The generator ObservablePropertyGenerator failed to execute on type {0}"</c>.
164+
/// </para>
165+
/// </summary>
166+
public static readonly DiagnosticDescriptor ObservablePropertyGeneratorError = new(
167+
id: "MVVMTK0010",
168+
title: $"Internal error for {nameof(ObservablePropertyGenerator)}",
169+
messageFormat: $"The generator {nameof(ObservablePropertyGenerator)} failed to execute on type {{0}}",
170+
category: typeof(ObservableObjectGenerator).FullName,
171+
defaultSeverity: DiagnosticSeverity.Error,
172+
isEnabledByDefault: true,
173+
description: $"The {nameof(ObservablePropertyGenerator)} generator encountered an error while processing a type. Please report this issue at https://aka.ms/mvvmtoolkit.",
174+
helpLinkUri: "https://aka.ms/mvvmtoolkit");
159175
}
160176
}

0 commit comments

Comments
 (0)