Refactor InvalidDomainValueException usage and messages - #29
Conversation
Refactored how InvalidDomainValueException is constructed and thrown across the codebase for consistency and clarity. Introduced static factory methods for common error scenarios (not initialized, string range, limit exceeded) and improved error message formatting to always include the type and value. Updated all generated domain primitive types to use the new pattern. Also made minor documentation and whitespace corrections.
There was a problem hiding this comment.
Pull request overview
This PR refactors the InvalidDomainValueException class to use static factory methods for common error scenarios and updates the error message format to consistently include type and value information. The refactoring improves code clarity and consistency across all generated domain primitive types.
Key Changes
- Introduced static factory methods (
NotInitializedException,StringRangeException,LimitExceededException) for creating exceptions - Updated exception constructors to accept
Typeandobject? valueparameters instead ofIDomainValue - Enhanced error message formatting to always include the domain type name and optionally the problematic value
Reviewed changes
Copilot reviewed 32 out of 33 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/AltaSoft.DomainPrimitives/InvalidDomainValueException.cs | Added static factory methods and new constructor overloads accepting Type/value parameters; removed old IDomainValue-based constructor |
| src/AltaSoft.DomainPrimitives.Generator/Executor.cs | Updated code generation to use new factory methods; added CultureInfo import for invariant culture formatting |
| src/AltaSoft.DomainPrimitives.Generator/Helpers/MethodGeneratorHelper.cs | Updated ValidateOrThrow generation to use new exception constructor signature; fixed documentation spacing |
| tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/*.g.verified.cs | Updated all 25+ snapshot test files to reflect new exception usage pattern and documentation fixes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public InvalidDomainValueException(string message, IDomainValue instance) : base(GenerateErrorMessage(message, instance)) | ||
| public InvalidDomainValueException(string message, Type type, object? value) : base(GenerateErrorMessage(message, type, value)) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generates the error message for the <see cref="InvalidDomainValueException"/>. | ||
| /// Initializes a new instance of the <see cref="InvalidDomainValueException"/> class with a specific error message. | ||
| /// </summary> | ||
| /// <param name="message">The error message that describes the reason for the exception.</param> | ||
| /// <param name="value">The actual value of the domain primitive.</param> | ||
| /// <returns>The generated error message.</returns> | ||
| private static string GenerateErrorMessage(string message, IDomainValue value) | ||
| /// <param name="type">The <see cref="Type"/> of the domain primitive that failed validation.</param> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public InvalidDomainValueException(string message, Type type) : base(GenerateErrorMessage(message, type)) | ||
| { | ||
| } |
There was a problem hiding this comment.
This is a breaking API change. The original constructor InvalidDomainValueException(string message, IDomainValue instance) has been completely removed and replaced with constructors that accept Type and object parameters. Any external code that manually constructs this exception using the old signature will break. Consider maintaining the old constructor as obsolete with a forwarding implementation, or clearly document this as a breaking change in release notes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Include domain primitive type in string length exception messages. - Quote string values and display (null) for nulls in error output. - Add explicit string length checks in constructors and TryCreate. - Introduce NestedString test class with [StringLength] attribute. - Ensure more robust and consistent enforcement of string constraints.
Refactored how InvalidDomainValueException is constructed and thrown across the codebase for consistency and clarity. Introduced static factory methods for common error scenarios (not initialized, string range, limit exceeded) and improved error message formatting to always include the type and value. Updated all generated domain primitive types to use the new pattern. Also made minor documentation and whitespace corrections.