Skip to content

Refactor InvalidDomainValueException usage and messages - #29

Merged
GregoryNikolaishvili merged 4 commits into
mainfrom
feature/ImrovedExceptions
Dec 15, 2025
Merged

GregoryNikolaishvili merged 4 commits into
mainfrom
feature/ImrovedExceptions

Conversation

@GregoryNikolaishvili

Copy link
Copy Markdown
Contributor

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.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Type and object? value parameters instead of IDomainValue
  • 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.

Comment thread src/AltaSoft.DomainPrimitives.Generator/Executor.cs Outdated
Comment thread src/AltaSoft.DomainPrimitives/InvalidDomainValueException.cs Outdated
Comment on lines 17 to +31
[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))
{
}

Copilot AI Dec 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
GregoryNikolaishvili and others added 3 commits December 15, 2025 14:41
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.
@GregoryNikolaishvili
GregoryNikolaishvili merged commit 2d35925 into main Dec 15, 2025
1 check passed
@GregoryNikolaishvili
GregoryNikolaishvili deleted the feature/ImrovedExceptions branch December 15, 2025 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants