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

Class ValueObjectErrors

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

Carries the code of a violated rule across an exception that a serializer, a data access library or any other integration throws in its own terms.

public static class ValueObjectErrors

Inheritance​

object ← ValueObjectErrors

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

An integration on a boundary reports a rejected value with the exception its ecosystem expects: a , a JsonSerializationException for Newtonsoft.Json, a DataException for Dapper. Those types have no place for a code, so the integrations store it in , under , and the System.Text.Json converters throw a , which carries it as a property as well. reads either, and a , from the exception or from one it wraps.

An exception a value object's rule raised carries the code and the value object type, never the value it refused. A DataAnnotations that the throwing Validator methods raise for a refusal of or carries both on its , and holds the value it was handed in , as DataAnnotations does for any attribute. The error of a database provider for a row a CHECK constraint from the rules refused, to which the Entity Framework Core package's UseValueObjectCheckErrors() adds the code of the constraint's rule, carries no type, and keeps the message its provider wrote, which may hold the row.

Fields​

ErrorCodeKey​

The key under which an integration stores the code of the violated rule in .

public const string ErrorCodeKey = "AdCodicem.ValueObjects.ErrorCode"

Field Value​

string

Remarks​

An integration of your own stores a code there the same way, so that , and anything that writes out the data of an exception, finds it: exception.Data[ValueObjectErrors.ErrorCodeKey] = code.

Methods​

TryGetCode(Exception, out string?)​

Reads the code of the rule an exception, or one of the exceptions it wraps, carries.

public static bool TryGetCode(Exception exception, out string? code)

Parameters​

exception Exception

The exception, as it was caught.

code string?

The code, or null when no exception of the chain carries one.

Returns​

bool

true when a code was found.

Remarks​

Each exception is read in turn, from exception down its chain, since a framework may wrap the exception that carries the code: a minimal API answers a body it cannot read with a BadHttpRequestException around the , and Entity Framework Core fails SaveChanges with a DbUpdateException around the . The first code found wins, read from , then , then the a DataAnnotations carries, which the throwing Validator.ValidateObject, ValidateProperty and ValidateValue raise for a refusal of or , then under .

An exception that reports no broken rule carries none. The error of a database provider for a row a CHECK constraint from the rules refused carries one under the Entity Framework Core package's UseValueObjectCheckErrors(), though no value object raised it, and a code of your own travels as the framework's do.

Exceptions​

ArgumentNullException

exception is null.