Skip to content

Commit c908dd7

Browse files
authored
Merge pull request #40 from ByronMayne/feature/exception-exposing
Exposed 'ExceptionHandler' event in IncrementalGenerator to write simpler unit tests
2 parents a1bd8a4 + 74619a1 commit c908dd7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/SourceGenerator.Foundations.Contracts/IncrementalGenerator.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public abstract class IncrementalGenerator : IDisposable
3030
/// </summary>
3131
public ILogger Logger { get; private set; } // Set with reflection, don't change
3232

33+
/// <summary>
34+
/// Allows you to set a custom exception handler for capturing exceptions
35+
/// that are thrown by the generator
36+
/// </summary>
37+
public event Action<Exception>? ExceptionHandler;
38+
3339

3440
static IncrementalGenerator()
3541
{
@@ -44,7 +50,8 @@ protected IncrementalGenerator(string? name)
4450
{
4551
Name = name ?? GetType().Name;
4652
Logger = new Logger(Name);
47-
if(s_environment != null)
53+
ExceptionHandler += OnException;
54+
if (s_environment != null)
4855
{
4956
foreach(ILogSink sink in s_environment.GetLogSinks())
5057
{
@@ -97,7 +104,7 @@ private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
97104
{
98105
if (e.ExceptionObject is Exception exception)
99106
{
100-
OnException(exception);
107+
ExceptionHandler?.Invoke(exception);
101108
}
102109
}
103110

0 commit comments

Comments
 (0)