| | | 1 | | namespace NexusLabs.Needlr; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Exception thrown when Needlr container verification detects one or more registered issues |
| | | 5 | | /// that are configured to fail-fast at startup. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <remarks> |
| | | 8 | | /// <para> |
| | | 9 | | /// Container verification is triggered by calling <c>VerifyContainer()</c> (or the equivalent |
| | | 10 | | /// extension) after building the service provider. Each <see cref="VerificationIssue"/> in |
| | | 11 | | /// <see cref="Issues"/> describes one problem — for example, a missing required dependency or |
| | | 12 | | /// a misconfigured lifetime. |
| | | 13 | | /// </para> |
| | | 14 | | /// <para> |
| | | 15 | | /// Only issues whose configured severity is set to throw will produce this exception. |
| | | 16 | | /// Issues configured to warn are reported through the logging pipeline instead. |
| | | 17 | | /// </para> |
| | | 18 | | /// </remarks> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// try |
| | | 22 | | /// { |
| | | 23 | | /// var provider = services.BuildServiceProvider(); |
| | | 24 | | /// provider.VerifyContainer(); |
| | | 25 | | /// } |
| | | 26 | | /// catch (ContainerVerificationException ex) |
| | | 27 | | /// { |
| | | 28 | | /// foreach (var issue in ex.Issues) |
| | | 29 | | /// { |
| | | 30 | | /// Console.Error.WriteLine($"[{issue.Type}] {issue.Message}"); |
| | | 31 | | /// } |
| | | 32 | | /// throw; |
| | | 33 | | /// } |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | public sealed class ContainerVerificationException : Exception |
| | | 37 | | { |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the verification issues that caused this exception. |
| | | 40 | | /// </summary> |
| | 7 | 41 | | public IReadOnlyList<VerificationIssue> Issues { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Creates a new container verification exception. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="issues">The verification issues that caused the exception.</param> |
| | | 47 | | public ContainerVerificationException(IReadOnlyList<VerificationIssue> issues) |
| | 5 | 48 | | : base(FormatMessage(issues)) |
| | | 49 | | { |
| | 5 | 50 | | Issues = issues; |
| | 5 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static string FormatMessage(IReadOnlyList<VerificationIssue> issues) |
| | | 54 | | { |
| | 11 | 55 | | var grouped = issues.GroupBy(i => i.Type); |
| | 5 | 56 | | var parts = new List<string>(); |
| | | 57 | | |
| | 20 | 58 | | foreach (var group in grouped) |
| | | 59 | | { |
| | 5 | 60 | | parts.Add($"{group.Count()} {group.Key} issue(s)"); |
| | | 61 | | } |
| | | 62 | | |
| | 5 | 63 | | return $"Container verification failed: {string.Join(", ", parts)}. " + |
| | 5 | 64 | | $"See the {nameof(Issues)} property for details."; |
| | | 65 | | } |
| | | 66 | | } |