using AutoMapper;
using AutoMapper.Internal;
namespace DigitalData.Core.Exceptions
{
///
/// 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.
///
public class MappingResultNullException : AutoMapperMappingException
{
///
/// Initializes a new instance of the class with the source and destination types causing the error.
///
/// The source type of the mapping operation.
/// The destination type of the mapping operation.
/// The exception that is the cause of the current exception, or a null reference if no inner exception is specified.
public MappingResultNullException(Type sourceType, Type destinationType, Exception? innerException = null)
: base(FormatMessage(sourceType, destinationType), innerException, new TypePair(sourceType, destinationType))
{
}
///
/// Formats the exception message to include detailed information about the mapping operation that failed.
///
/// The source type involved in the mapping operation.
/// The destination type involved in the mapping operation.
/// A formatted string containing the error message.
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.";
}
}
}