using System; using System.Runtime.Serialization; namespace Umbraco.Core.Persistence { // TODO: Would be good to use this exception type anytime we cannot find an entity /// /// An exception used to indicate that an Umbraco entity could not be found. /// /// [Obsolete("Instead of throwing an exception, return null or an HTTP 404 status code instead.")] [Serializable] public class EntityNotFoundException : Exception { /// /// Gets the identifier. /// /// /// The identifier. /// /// /// This object should be serializable to prevent a to be thrown. /// public object Id { get; private set; } /// /// Initializes a new instance of the class. /// public EntityNotFoundException() { } /// /// Initializes a new instance of the class. /// /// The identifier. /// The message. public EntityNotFoundException(object id, string message) : base(message) { Id = id; } /// /// Initializes a new instance of the class. /// /// The message that describes the error. public EntityNotFoundException(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 EntityNotFoundException(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 EntityNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { Id = info.GetValue(nameof(Id), typeof(object)); } /// /// 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(Id), Id); base.GetObjectData(info, context); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { var result = base.ToString(); if (Id != null) { return "Umbraco entity (id: " + Id + ") not found. " + result; } return result; } } }