Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectException

Namespace: AdCodicem.ValueObjects
Assembly: AdCodicem.ValueObjects.Abstractions.dll

Thrown when a value object is constructed from a value that violates one of its rules.

[Serializable]
public class ValueObjectException : FormatException, ISerializable

Inheritance​

object ← Exception ← SystemException ← FormatException ← ValueObjectException

Implements​

ISerializable

Inherited Members​

Exception.GetBaseException(), Exception.GetType(), Exception.ToString(), Exception.Data, Exception.HelpLink, Exception.HResult, Exception.InnerException, Exception.Message, Exception.Source, Exception.StackTrace, Exception.TargetSite, Exception.SerializeObjectState, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

The integrations that take outside input (JSON, model binding, FluentValidation, Dapper) use the TryCreate and TryParse family and report a refusal in their own terms. This exception comes from the explicit Create and Parse entry points and an explicit conversion, for code that treats a rejected value as a bug, from a strict EF Core read, which goes through Create and fails the query, and from an EF Core write of an uninitialized value object whose value its type rejects, which fails SaveChanges.

It is a , the exception documents for text it refuses, so code written against that contract catches a value object's rejection, and a framework that recognizes a as bad input, such as ASP.NET Core MVC binding through a type converter, reports it as such. Every path throws this one type, including Create and the other paths that take a value rather than text. A catch (FormatException) therefore catches it too, and placed before a catch (ValueObjectException) of the same try, makes that clause unreachable (CS0160).

The message names the value object and the rule, never the rejected value, so a log recording it records no IBAN, email address or telephone number. does hold the value, except on a value object its author classifies as sensitive data: see that property.

Constructors​

ValueObjectException()​

Initializes a new instance of the class.

public ValueObjectException()

ValueObjectException(string)​

Initializes a new instance of the class.

public ValueObjectException(string message)

Parameters​

message string

Message describing the violated rule.

ValueObjectException(string, Exception)​

Initializes a new instance of the class.

public ValueObjectException(string message, Exception innerException)

Parameters​

message string

Message describing the violated rule.

innerException Exception

Cause of this exception.

ValueObjectException(string, Type?, string?, object?)​

Initializes a new instance of the class.

public ValueObjectException(string message, Type? valueObjectType, string? errorCode, object? attemptedValue)

Parameters​

message string

Message describing the violated rule.

valueObjectType Type?

Value object type that rejected the value.

errorCode string?

Stable, machine-readable code of the violated rule.

attemptedValue object?

Value that was rejected.

Properties​

AttemptedValue​

Gets the value that was rejected.

public object? AttemptedValue { get; }

Property Value​

object?

Remarks​

The value as it was passed in, before normalization, and the text for Parse. Unlike the message, it holds the value itself: a logger that records the public properties of an exception, as Serilog.Exceptions does, records it in clear.

The generated Create and Parse leave it null on a value object that carries an attribute derived from Microsoft.Extensions.Compliance.Classification.DataClassificationAttribute, other than NoDataClassificationAttribute, and so does the type converter of a generic one: classify a value object that holds personal data.

ErrorCode​

Gets the stable, machine-readable code of the violated rule.

public string? ErrorCode { get; }

Property Value​

string?

ValueObjectType​

Gets the value object type that rejected the value.

public Type? ValueObjectType { get; }

Property Value​

Type?

Methods​

Throw(Type, string, string, object?)​

Throws a describing a rejected value.

[DoesNotReturn]
public static void Throw(Type valueObjectType, string errorCode, string errorMessage, object? attemptedValue)

Parameters​

valueObjectType Type

Value object type that rejected the value.

errorCode string

Stable, machine-readable code of the violated rule.

errorMessage string

Human-readable description of the violated rule.

attemptedValue object?

Value that was rejected.

Exceptions​

ValueObjectException

Always.