using System; using System.Runtime.Serialization; namespace Umbraco.Core.Exceptions { /// /// The exception that is thrown when a composition is invalid. /// /// [Serializable] public class InvalidCompositionException : Exception { /// /// Gets the content type alias. /// /// /// The content type alias. /// public string ContentTypeAlias { get; } /// /// Gets the added composition alias. /// /// /// The added composition alias. /// public string AddedCompositionAlias { get; } /// /// Gets the property type aliases. /// /// /// The property type aliases. /// public string[] PropertyTypeAliases { get; } /// /// Initializes a new instance of the class. /// public InvalidCompositionException() { } /// /// Initializes a new instance of the class. /// /// The content type alias. /// The property type aliases. public InvalidCompositionException(string contentTypeAlias, string[] propertyTypeAliases) : this(contentTypeAlias, null, propertyTypeAliases) { } /// /// Initializes a new instance of the class. /// /// The content type alias. /// The added composition alias. /// The property type aliases. public InvalidCompositionException(string contentTypeAlias, string addedCompositionAlias, string[] propertyTypeAliases) : this(addedCompositionAlias.IsNullOrWhiteSpace() ? string.Format( "ContentType with alias '{0}' has an invalid composition " + "and there was a conflict on the following PropertyTypes: '{1}'. " + "PropertyTypes must have a unique alias across all Compositions in order to compose a valid ContentType Composition.", contentTypeAlias, string.Join(", ", propertyTypeAliases)) : string.Format( "ContentType with alias '{0}' was added as a Composition to ContentType with alias '{1}', " + "but there was a conflict on the following PropertyTypes: '{2}'. " + "PropertyTypes must have a unique alias across all Compositions in order to compose a valid ContentType Composition.", addedCompositionAlias, contentTypeAlias, string.Join(", ", propertyTypeAliases))) { ContentTypeAlias = contentTypeAlias; AddedCompositionAlias = addedCompositionAlias; PropertyTypeAliases = propertyTypeAliases; } /// /// Initializes a new instance of the class. /// /// The message that describes the error. public InvalidCompositionException(string message) : base(message) { } /// /// Initializes a new instance of the class. /// /// The error message that explains the reason for the exception. /// The exception that is the cause of the current exception, or a null reference ( in Visual Basic) if no inner exception is specified. public InvalidCompositionException(string message, Exception innerException) : base(message, innerException) { } /// /// Initializes a new instance of the class. /// /// The that holds the serialized object data about the exception being thrown. /// The that contains contextual information about the source or destination. protected InvalidCompositionException(SerializationInfo info, StreamingContext context) : base(info, context) { ContentTypeAlias = info.GetString(nameof(ContentTypeAlias)); AddedCompositionAlias = info.GetString(nameof(AddedCompositionAlias)); PropertyTypeAliases = (string[])info.GetValue(nameof(PropertyTypeAliases), typeof(string[])); } /// /// When overridden in a derived class, sets the with information about the exception. /// /// The that holds the serialized object data about the exception being thrown. /// The that contains contextual information about the source or destination. /// info public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException(nameof(info)); } info.AddValue(nameof(ContentTypeAlias), ContentTypeAlias); info.AddValue(nameof(AddedCompositionAlias), AddedCompositionAlias); info.AddValue(nameof(PropertyTypeAliases), PropertyTypeAliases); base.GetObjectData(info, context); } } }