Repository navigation
feat(core): allow custom subtypes of built-in errors #469
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
9 commits
Select commit
Hold shift + click to select a range
fa887e9
feat(core): allow custom subtypes of built-in errors
Reefact 262c608
test(core): drop invalid new modifier on custom error factories
claude e3c1f30
test(core): cover custom error subtypes from a consumer assembly
claude 266760c
docs: describe specializing the built-in error types
claude 7821b51
docs: accept ADR-0072 and close its follow-up actions
claude e4fda6f
docs: fix the French wording of ADR-0072
claude f2a346b
test(core): cover a specialized exception on a custom error
claude 078f2a5
docs: correct the extension guide on metadata and ToException
claude 4c9b3cc
docs: scope the ADR-0072 rationale to infrastructure-derived types
claude 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
107 changes: 107 additions & 0 deletions
107
FirstClassErrors.GenDoc.UnitTests/CustomErrorTypesTests.cs
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,107 @@ | ||
| #region Usings declarations | ||
|
|
||
| using NFluent; | ||
|
|
||
| #endregion | ||
|
|
||
| namespace FirstClassErrors.GenDoc.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Proves, from an assembly that has no access to the library's <c>internal</c> members, that the built-in error | ||
| /// types can be specialized by a consumer (ADR-0072) and that GenDoc documents such specialized errors like any other. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This project is deliberately the vantage point: the library only exposes its internals to | ||
| /// <c>FirstClassErrors.UnitTests</c> and <c>FirstClassErrors.Testing</c>, so a fixture that compiles here is a fixture a | ||
| /// real consumer can write. | ||
| /// </remarks> | ||
| public sealed class CustomErrorTypesTests { | ||
|
|
||
| #region Statics members declarations | ||
|
|
||
| private static ErrorDocumentationExtractionResult Extract() { | ||
| return AssemblyErrorDocumentationReader.GetErrorDocumentationFrom(typeof(CustomErrorTypesTests).Assembly); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| [Fact(DisplayName = "A consumer assembly can derive a typed error and its exception from the built-in ones, and build it through its own staged factory.")] | ||
| public void AConsumerAssemblyCanDeriveATypedError() { | ||
| // Exercise | ||
| CustomPersistenceError error = CustomRepositoryErrors.Unavailable(); | ||
|
|
||
| // Verify | ||
| Check.That(error.Code).IsEqualTo(ErrorCode.Create("CUSTOM_REPOSITORY_UNAVAILABLE")); | ||
| Check.That(error.ShortMessage).IsEqualTo("Service temporarily unavailable."); | ||
| Check.That(error.Direction).IsEqualTo(InteractionDirection.Outgoing); | ||
| Check.That(error.Transience).IsEqualTo(Transience.Transient); | ||
| Check.That(error.ToException()).IsInstanceOf<CustomPersistenceException>(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "GenDoc extracts a documented factory that returns a custom error type, running its examples.")] | ||
| public void GenDocExtractsADocumentedFactoryReturningACustomErrorType() { | ||
| // Exercise | ||
| ErrorDocumentationExtractionResult extraction = Extract(); | ||
|
|
||
| // Verify | ||
| ErrorDocumentation? documentation = extraction.Documentation.FirstOrDefault(doc => doc.Code == "CUSTOM_REPOSITORY_UNAVAILABLE"); | ||
|
|
||
| Check.That(documentation).IsNotNull(); | ||
| Check.That(documentation!.Title).IsEqualTo("Custom repository unavailable"); | ||
| Check.That(documentation.Source).IsEqualTo("CustomRepository"); | ||
| Check.That(documentation.Examples).CountIs(1); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Extracting a custom error type records no failure.")] | ||
| public void ExtractingACustomErrorTypeRecordsNoFailure() { | ||
| // Exercise | ||
| ErrorDocumentationExtractionResult extraction = Extract(); | ||
|
|
||
| // Verify | ||
| Check.That(extraction.Failures).IsEmpty(); | ||
| } | ||
|
|
||
| private sealed class CustomPersistenceError : SecondaryPortError { | ||
|
|
||
| private CustomPersistenceError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage, Transience transience) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage, transience) { } | ||
|
|
||
| public static PublicMessageStage<CustomPersistenceError> Create(ErrorCode code, string diagnosticMessage, Transience transience) { | ||
| return new PublicMessageStage<CustomPersistenceError>((shortMessage, detailedMessage) => | ||
| new CustomPersistenceError(code, diagnosticMessage, shortMessage, detailedMessage, transience)); | ||
| } | ||
|
|
||
| public override DiagnosableException ToException() { | ||
| return new CustomPersistenceException(this); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private sealed class CustomPersistenceException : SecondaryPortException { | ||
|
|
||
| public CustomPersistenceException(CustomPersistenceError error) : base(error) { } | ||
|
|
||
| } | ||
|
|
||
| [ProvidesErrorsFor("CustomRepository", Description = "Errors raised by the custom repository fixture.")] | ||
| private static class CustomRepositoryErrors { | ||
|
|
||
| [DocumentedBy(nameof(UnavailableDocumentation))] | ||
| public static CustomPersistenceError Unavailable() { | ||
| return CustomPersistenceError.Create(ErrorCode.Create("CUSTOM_REPOSITORY_UNAVAILABLE"), | ||
| "The custom repository is unavailable.", | ||
| Transience.Transient) | ||
| .WithPublicMessage("Service temporarily unavailable."); | ||
| } | ||
|
|
||
| private static ErrorDocumentation UnavailableDocumentation() { | ||
| return DescribeError.WithTitle("Custom repository unavailable") | ||
| .WithDescription("This error occurs when the custom repository cannot be reached.") | ||
| .WithoutRule() | ||
| .WithoutDiagnostic() | ||
| .WithExamples(Unavailable); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
157 changes: 157 additions & 0 deletions
157
FirstClassErrors.UnitTests/ExtensibleErrorTypesTests.cs
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,157 @@ | ||
| #region Usings declarations | ||
|
|
||
| using FirstClassErrors.Testing; | ||
|
|
||
| using NFluent; | ||
|
|
||
| #endregion | ||
|
|
||
| namespace FirstClassErrors.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Exercises custom error types as they would be defined in an application referencing FirstClassErrors. | ||
| /// </summary> | ||
| public sealed class ExtensibleErrorTypesTests { | ||
|
|
||
| [Fact(DisplayName = "Derived domain errors retain their type and diagnostic fields.")] | ||
| public void DerivedDomainErrorRetainsItsTypeAndDiagnosticFields() { | ||
| // Exercise | ||
| ErrorCode code = ErrorCodeFactory.Any(); | ||
| CustomDomainError error = CustomDomainError.Create(code, "Internal diagnostic") | ||
| .WithPublicMessage("Public summary", "Public details"); | ||
|
|
||
| // Verify | ||
| Check.That(error.Code).IsEqualTo(code); | ||
| Check.That(error.DiagnosticMessage).IsEqualTo("Internal diagnostic"); | ||
| Check.That(error.ShortMessage).IsEqualTo("Public summary"); | ||
| Check.That(error.DetailedMessage).IsEqualTo("Public details"); | ||
| Check.That(error.ToException() is DomainException).IsTrue(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Derived infrastructure errors preserve direction and transience.")] | ||
| public void DerivedInfrastructureErrorPreservesMetadata() { | ||
| // Exercise | ||
| CustomInfrastructureError error = CustomInfrastructureError.Create(ErrorCodeFactory.Any(), "Diagnostic") | ||
| .WithPublicMessage("Unavailable"); | ||
|
|
||
| // Verify | ||
| Check.That(error.Direction).IsEqualTo(InteractionDirection.Outgoing); | ||
| Check.That(error.Transience).IsEqualTo(Transience.Transient); | ||
| Check.That(error.ToException() is InfrastructureException).IsTrue(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Derived primary port errors aggregate nested errors without losing direction.")] | ||
| public void DerivedPrimaryPortErrorAggregatesInnerErrors() { | ||
| // Setup | ||
| DomainError inner = ErrorFactory.Domain(ErrorCodeFactory.Any(), "Invalid input"); | ||
| PrimaryPortInnerErrors innerErrors = new PrimaryPortInnerErrors().Add(inner); | ||
|
|
||
| // Exercise | ||
| CustomPrimaryPortError error = CustomPrimaryPortError.Create(ErrorCodeFactory.Any(), "Diagnostic", innerErrors) | ||
| .WithPublicMessage("Invalid request"); | ||
|
|
||
| // Verify | ||
| Check.That(error.Direction).IsEqualTo(InteractionDirection.Incoming); | ||
| Check.That(error.Transience).IsEqualTo(Transience.Unknown); | ||
| Check.That(error.InnerErrors).CountIs(1); | ||
| Check.That(error.InnerErrors[0]).IsSameReferenceAs(inner); | ||
| Check.That(error.ToException() is PrimaryPortException).IsTrue(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "A repository factory can produce a derived secondary port error.")] | ||
| public void RepositoryFactoryProducesTypedPersistenceError() { | ||
| // Exercise | ||
| PersistenceError error = CommandRepositoryErrors.Unavailable(); | ||
|
|
||
| // Verify | ||
| Check.That(error.Code).IsEqualTo(ErrorCode.Create("COMMAND_REPOSITORY_UNAVAILABLE")); | ||
| Check.That(error.Direction).IsEqualTo(InteractionDirection.Outgoing); | ||
| Check.That(error.Transience).IsEqualTo(Transience.Transient); | ||
| Check.That(error.ToException() is SecondaryPortException).IsTrue(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Derived secondary port errors preserve nested outgoing errors and computed transience.")] | ||
| public void DerivedSecondaryPortErrorAggregatesInnerErrors() { | ||
| // Setup | ||
| SecondaryPortError inner = ErrorFactory.Secondary(ErrorCodeFactory.Any(), "Dependency failed", Transience.NonTransient); | ||
| SecondaryPortInnerErrors innerErrors = new SecondaryPortInnerErrors().Add(inner); | ||
|
|
||
| // Exercise | ||
| PersistenceError error = PersistenceError.Create(ErrorCodeFactory.Any(), "Repository failed", innerErrors) | ||
| .WithPublicMessage("Repository unavailable"); | ||
|
|
||
| // Verify | ||
| Check.That(error.Direction).IsEqualTo(InteractionDirection.Outgoing); | ||
| Check.That(error.Transience).IsEqualTo(Transience.NonTransient); | ||
| Check.That(error.InnerErrors).CountIs(1); | ||
| Check.That(error.InnerErrors[0]).IsSameReferenceAs(inner); | ||
| } | ||
|
|
||
| private sealed class CustomDomainError : DomainError { | ||
|
|
||
| private CustomDomainError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage) { } | ||
|
|
||
| public static PublicMessageStage<CustomDomainError> Create(ErrorCode code, string diagnosticMessage) { | ||
| return new PublicMessageStage<CustomDomainError>((shortMessage, detailedMessage) => | ||
| new CustomDomainError(code, diagnosticMessage, shortMessage, detailedMessage)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private sealed class CustomInfrastructureError : InfrastructureError { | ||
|
|
||
| private CustomInfrastructureError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage, InteractionDirection.Outgoing, Transience.Transient) { } | ||
|
|
||
| public static PublicMessageStage<CustomInfrastructureError> Create(ErrorCode code, string diagnosticMessage) { | ||
| return new PublicMessageStage<CustomInfrastructureError>((shortMessage, detailedMessage) => | ||
| new CustomInfrastructureError(code, diagnosticMessage, shortMessage, detailedMessage)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private sealed class CustomPrimaryPortError : PrimaryPortError { | ||
|
|
||
| private CustomPrimaryPortError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage, PrimaryPortInnerErrors innerErrors) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage, innerErrors) { } | ||
|
|
||
| public static PublicMessageStage<CustomPrimaryPortError> Create(ErrorCode code, string diagnosticMessage, PrimaryPortInnerErrors innerErrors) { | ||
| return new PublicMessageStage<CustomPrimaryPortError>((shortMessage, detailedMessage) => | ||
| new CustomPrimaryPortError(code, diagnosticMessage, shortMessage, detailedMessage, innerErrors)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private sealed class PersistenceError : SecondaryPortError { | ||
|
|
||
| private PersistenceError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage, Transience transience) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage, transience) { } | ||
|
|
||
| private PersistenceError(ErrorCode code, string diagnosticMessage, string shortMessage, string? detailedMessage, SecondaryPortInnerErrors innerErrors) | ||
| : base(code, diagnosticMessage, shortMessage, detailedMessage, innerErrors) { } | ||
|
|
||
| public static PublicMessageStage<PersistenceError> Create(ErrorCode code, string diagnosticMessage, Transience transience) { | ||
| return new PublicMessageStage<PersistenceError>((shortMessage, detailedMessage) => | ||
| new PersistenceError(code, diagnosticMessage, shortMessage, detailedMessage, transience)); | ||
| } | ||
|
|
||
| public static PublicMessageStage<PersistenceError> Create(ErrorCode code, string diagnosticMessage, SecondaryPortInnerErrors innerErrors) { | ||
| return new PublicMessageStage<PersistenceError>((shortMessage, detailedMessage) => | ||
| new PersistenceError(code, diagnosticMessage, shortMessage, detailedMessage, innerErrors)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static class CommandRepositoryErrors { | ||
|
|
||
| public static PersistenceError Unavailable() { | ||
| return PersistenceError.Create(ErrorCode.Create("COMMAND_REPOSITORY_UNAVAILABLE"), | ||
| "The command repository is unavailable.", | ||
| Transience.Transient) | ||
| .WithPublicMessage("Service temporarily unavailable."); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
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
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
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
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.