using System; using System.Collections.Generic; using System.Net; using System.Runtime.Serialization; namespace Umbraco.Web.Common.Exceptions { [Serializable] public class HttpResponseException : Exception { public HttpResponseException(HttpStatusCode status = HttpStatusCode.InternalServerError, object value = null) { Status = status; Value = value; } public HttpStatusCode Status { get; set; } public object Value { get; set; } public IDictionary AdditionalHeaders { get; } = new Dictionary(); /// /// 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(Status), Enum.GetName(typeof(HttpStatusCode), Status)); info.AddValue(nameof(Value), Value); info.AddValue(nameof(AdditionalHeaders), AdditionalHeaders); base.GetObjectData(info, context); } public static HttpResponseException CreateValidationErrorResponse(object model) { return new HttpResponseException(HttpStatusCode.BadRequest, model) { AdditionalHeaders = { ["X-Status-Reason"] = "Validation failed" } }; } } }