DigitalData.Core/DigitalData.Core.Exceptions/MappingResultNullException.cs
2024-03-06 16:14:36 +01:00

36 lines
2.1 KiB
C#

using AutoMapper;
using AutoMapper.Internal;
namespace DigitalData.Core.Exceptions
{
/// <summary>
/// Represents errors that occur during mapping when the result is unexpectedly null.
/// This exception is specifically used to indicate that an AutoMapper mapping operation
/// resulted in a null object, which may suggest a misconfiguration or an issue with the source data.
/// </summary>
public class MappingResultNullException : AutoMapperMappingException
{
/// <summary>
/// Initializes a new instance of the <see cref="MappingResultNullException"/> class with the source and destination types causing the error.
/// </summary>
/// <param name="sourceType">The source type of the mapping operation.</param>
/// <param name="destinationType">The destination type of the mapping operation.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public MappingResultNullException(Type sourceType, Type destinationType, Exception? innerException = null)
: base(FormatMessage(sourceType, destinationType), innerException, new TypePair(sourceType, destinationType))
{
}
/// <summary>
/// Formats the exception message to include detailed information about the mapping operation that failed.
/// </summary>
/// <param name="sourceType">The source type involved in the mapping operation.</param>
/// <param name="destinationType">The destination type involved in the mapping operation.</param>
/// <returns>A formatted string containing the error message.</returns>
private static string FormatMessage(Type sourceType, Type destinationType)
{
return $"Mapping from {sourceType.FullName} to {destinationType.FullName} resulted in a null object. " +
"Hint: Ensure that the AutoMapper profile configuration for this mapping is correct.";
}
}
}