-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add comprehensive error handling and logging (task 7.2) #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3cbb43a
feat: Add comprehensive error handling and logging (task 7.2)
jbrinkman 665e876
fix: add proper error handling for file stream reads in DLL validation
jbrinkman 82803d7
fix: add missing file read validation checks in assembly loader
jbrinkman f102d66
Update src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs
jbrinkman b48bd68
Update src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs
jbrinkman 6e193d6
Update src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs
jbrinkman a1050c7
Update src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs
jbrinkman 749f302
Update src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs
jbrinkman 08341f0
Update src/DotNetApiDiff/ExitCodes/GlobalExceptionHandler.cs
jbrinkman 333eeb8
fix: remove unnecessary native DLL caching in assembly loader
jbrinkman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,193 @@ | ||
| // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT | ||
| using System.Reflection; | ||
| using System.Runtime.ExceptionServices; | ||
| using System.Security; | ||
| using DotNetApiDiff.Interfaces; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace DotNetApiDiff.ExitCodes | ||
| { | ||
| /// <summary> | ||
| /// Provides centralized exception handling for the application. | ||
| /// </summary> | ||
| public class GlobalExceptionHandler : IGlobalExceptionHandler | ||
| { | ||
| private readonly ILogger<GlobalExceptionHandler> _logger; | ||
| private readonly IExitCodeManager _exitCodeManager; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GlobalExceptionHandler"/> class. | ||
| /// </summary> | ||
| /// <param name="logger">The logger to use for logging exceptions.</param> | ||
| /// <param name="exitCodeManager">The exit code manager to determine appropriate exit codes.</param> | ||
| public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger, IExitCodeManager exitCodeManager) | ||
| { | ||
| _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
| _exitCodeManager = exitCodeManager ?? throw new ArgumentNullException(nameof(exitCodeManager)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Handles an exception by logging it and determining the appropriate exit code. | ||
| /// </summary> | ||
| /// <param name="exception">The exception to handle.</param> | ||
| /// <param name="context">Optional context information about where the exception occurred.</param> | ||
| /// <returns>The appropriate exit code for the exception.</returns> | ||
| public int HandleException(Exception exception, string? context = null) | ||
| { | ||
| if (exception == null) | ||
| { | ||
| _logger.LogError("HandleException called with null exception"); | ||
| return _exitCodeManager.GetExitCodeForException(new ArgumentNullException(nameof(exception))); | ||
| } | ||
|
|
||
| // Log the exception with context if provided | ||
| if (!string.IsNullOrEmpty(context)) | ||
| { | ||
| _logger.LogError(exception, "Error in {Context}: {Message}", context, exception.Message); | ||
| } | ||
| else | ||
| { | ||
| _logger.LogError(exception, "Error: {Message}", exception.Message); | ||
| } | ||
|
|
||
| // Log additional details for specific exception types | ||
| LogExceptionDetails(exception); | ||
|
|
||
| // Determine the appropriate exit code | ||
| int exitCode = _exitCodeManager.GetExitCodeForException(exception); | ||
|
|
||
| _logger.LogInformation( | ||
| "Exiting with code {ExitCode}: {Description}", | ||
| exitCode, | ||
| _exitCodeManager.GetExitCodeDescription(exitCode)); | ||
|
|
||
| return exitCode; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Logs additional details for specific exception types. | ||
| /// </summary> | ||
| /// <param name="exception">The exception to log details for.</param> | ||
| private void LogExceptionDetails(Exception exception) | ||
| { | ||
| switch (exception) | ||
| { | ||
| case ReflectionTypeLoadException typeLoadEx: | ||
| LogReflectionTypeLoadException(typeLoadEx); | ||
| break; | ||
| case AggregateException aggregateEx: | ||
| LogAggregateException(aggregateEx); | ||
| break; | ||
| case FileNotFoundException fileNotFoundEx: | ||
| _logger.LogError("File not found: {FileName}", fileNotFoundEx.FileName); | ||
| break; | ||
| case BadImageFormatException badImageEx: | ||
| _logger.LogError("Bad image format: {FileName}", badImageEx.FileName); | ||
| break; | ||
| case SecurityException securityEx: | ||
| _logger.LogError("Security exception: {PermissionType}", securityEx.PermissionType); | ||
| break; | ||
| case InvalidOperationException: | ||
| // Log the stack trace for InvalidOperationException to help diagnose the issue | ||
| _logger.LogDebug("Stack trace: {StackTrace}", exception.StackTrace); | ||
| break; | ||
| } | ||
|
|
||
| // Log inner exception if present | ||
| if (exception.InnerException != null) | ||
| { | ||
| _logger.LogDebug("Inner exception: {Message}", exception.InnerException.Message); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Logs details for a ReflectionTypeLoadException. | ||
| /// </summary> | ||
| /// <param name="exception">The ReflectionTypeLoadException to log details for.</param> | ||
| private void LogReflectionTypeLoadException(ReflectionTypeLoadException exception) | ||
| { | ||
| _logger.LogError("ReflectionTypeLoadException: Failed to load {Count} types", exception.Types?.Length ?? 0); | ||
|
|
||
| if (exception.LoaderExceptions != null) | ||
| { | ||
| int loaderExceptionCount = exception.LoaderExceptions.Length; | ||
| _logger.LogError("Loader exceptions count: {Count}", loaderExceptionCount); | ||
|
|
||
| // Log up to 5 loader exceptions to avoid excessive logging | ||
| int logCount = Math.Min(loaderExceptionCount, 5); | ||
| for (int i = 0; i < logCount; i++) | ||
| { | ||
| var loaderEx = exception.LoaderExceptions[i]; | ||
| if (loaderEx != null) | ||
| { | ||
| _logger.LogError(loaderEx, "Loader exception {Index}: {Message}", i + 1, loaderEx.Message); | ||
| } | ||
| } | ||
|
|
||
| if (loaderExceptionCount > logCount) | ||
| { | ||
| _logger.LogError("... and {Count} more loader exceptions", loaderExceptionCount - logCount); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Logs details for an AggregateException. | ||
| /// </summary> | ||
| /// <param name="exception">The AggregateException to log details for.</param> | ||
| private void LogAggregateException(AggregateException exception) | ||
| { | ||
| _logger.LogError("AggregateException with {Count} inner exceptions", exception.InnerExceptions.Count); | ||
|
|
||
| // Log up to 5 inner exceptions to avoid excessive logging | ||
| int logCount = Math.Min(exception.InnerExceptions.Count, 5); | ||
| for (int i = 0; i < logCount; i++) | ||
| { | ||
| var innerEx = exception.InnerExceptions[i]; | ||
| _logger.LogError(innerEx, "Inner exception {Index}: {Message}", i + 1, innerEx.Message); | ||
| } | ||
|
|
||
| if (exception.InnerExceptions.Count > logCount) | ||
| { | ||
| _logger.LogError("... and {Count} more inner exceptions", exception.InnerExceptions.Count - logCount); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets up global unhandled exception handling. | ||
| /// </summary> | ||
| public void SetupGlobalExceptionHandling() | ||
| { | ||
| // Handle unhandled exceptions in the current AppDomain | ||
| AppDomain.CurrentDomain.UnhandledException += (sender, e) => | ||
| { | ||
| if (e.ExceptionObject is Exception ex) | ||
| { | ||
| _logger.LogCritical(ex, "Unhandled exception in AppDomain: {Message}", ex.Message); | ||
| } | ||
| else | ||
| { | ||
| _logger.LogCritical("Unhandled non-exception object in AppDomain: {Object}", e.ExceptionObject); | ||
| } | ||
| }; | ||
|
|
||
| // Handle unhandled exceptions in tasks | ||
| TaskScheduler.UnobservedTaskException += (sender, e) => | ||
| { | ||
| _logger.LogCritical(e.Exception, "Unobserved task exception: {Message}", e.Exception.Message); | ||
| e.SetObserved(); // Mark as observed to prevent process termination | ||
| }; | ||
|
|
||
| // Handle first-chance exceptions (useful for debugging) | ||
| if (_logger.IsEnabled(LogLevel.Debug)) | ||
| { | ||
| // Register for FirstChanceException events only when debug logging is enabled | ||
| AppDomain.CurrentDomain.FirstChanceException += (sender, e) => | ||
| { | ||
| // Only log first-chance exceptions at debug level to avoid noise | ||
| _logger.LogDebug(e.Exception, "First chance exception: {Message}", e.Exception.Message); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,22 @@ | ||
| // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT | ||
| namespace DotNetApiDiff.Interfaces | ||
| { | ||
| /// <summary> | ||
| /// Interface for global exception handling. | ||
| /// </summary> | ||
| public interface IGlobalExceptionHandler | ||
| { | ||
| /// <summary> | ||
| /// Handles an exception by logging it and determining the appropriate exit code. | ||
| /// </summary> | ||
| /// <param name="exception">The exception to handle.</param> | ||
| /// <param name="context">Optional context information about where the exception occurred.</param> | ||
| /// <returns>The appropriate exit code for the exception.</returns> | ||
| int HandleException(Exception exception, string? context = null); | ||
|
|
||
| /// <summary> | ||
| /// Sets up global unhandled exception handling. | ||
| /// </summary> | ||
| void SetupGlobalExceptionHandling(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.