-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d302b1
commit 17905b0
Showing
5 changed files
with
108 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal class ExceptionHandler(ILogger<ExceptionHandler> logger) | ||
: IExceptionHandler | ||
{ | ||
public void SafeRun<T>(T state, Action<T> action) | ||
{ | ||
try | ||
{ | ||
action(state); | ||
} | ||
catch (AggregateException aggregateException) | ||
{ | ||
OnAggregateException(aggregateException); | ||
} | ||
catch (CompileErrorException compileException) | ||
{ | ||
OnCompileException(compileException); | ||
} | ||
catch (HandledException handledException) | ||
{ | ||
OnHandledException(handledException); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
throw; | ||
} | ||
catch (Exception exception) | ||
{ | ||
OnException(exception); | ||
} | ||
} | ||
|
||
private void OnAggregateException(AggregateException aggregateException) | ||
{ | ||
foreach (var exception in aggregateException.InnerExceptions) | ||
{ | ||
switch (exception) | ||
{ | ||
case CompileErrorException compileError: | ||
OnCompileException(compileError); | ||
break; | ||
|
||
case HandledException handledException: | ||
OnHandledException(handledException); | ||
break; | ||
|
||
default: | ||
OnException(exception); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private void OnCompileException(CompileErrorException exception) => | ||
logger.CompileError(exception.ErrorMessage, exception.Location, exception.Id); | ||
|
||
private void OnHandledException(HandledException handledException) => | ||
logger.Log( | ||
new LogEntry( | ||
#if DEBUG | ||
DiagnosticSeverity.Info, | ||
#else | ||
DiagnosticSeverity.Hidden, | ||
#endif | ||
"Code generation aborted.", | ||
default, | ||
LogId.InfoGenerationInterrupted, | ||
handledException)); | ||
|
||
private void OnException(Exception exception) => | ||
logger.Log( | ||
new LogEntry( | ||
DiagnosticSeverity.Error, | ||
"An unhandled error has occurred.", | ||
default, | ||
LogId.ErrorUnhandled, | ||
exception)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal interface IExceptionHandler | ||
{ | ||
void SafeRun<T>(T state, Action<T> action); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters