using System; using System.Net; namespace EmbedIO { /// /// When thrown, breaks the request handling control flow /// and sends an error response to the client. /// #pragma warning disable CA1032 // Implement standard exception constructors - they have no meaning here. public partial class HttpException : Exception, IHttpException #pragma warning restore CA1032 { /// /// Initializes a new instance of the class, /// with no message to include in the response. /// /// The status code to set on the response. public HttpException(int statusCode) { StatusCode = statusCode; } /// /// Initializes a new instance of the class, /// with no message to include in the response. /// /// The status code to set on the response. public HttpException(HttpStatusCode statusCode) : this((int)statusCode) { } /// /// Initializes a new instance of the class, /// with a message to include in the response. /// /// The status code to set on the response. /// A message to include in the response as plain text. public HttpException(int statusCode, string? message) : base(message) { StatusCode = statusCode; HttpExceptionMessage = message; } /// /// Initializes a new instance of the class, /// with a message to include in the response. /// /// The status code to set on the response. /// A message to include in the response as plain text. public HttpException(HttpStatusCode statusCode, string? message) : this((int)statusCode, message) { } /// /// Initializes a new instance of the class, /// with a message and a data object to include in the response. /// /// The status code to set on the response. /// A message to include in the response as plain text. /// The data object to include in the response. public HttpException(int statusCode, string? message, object? data) : this(statusCode, message) { DataObject = data; } /// /// Initializes a new instance of the class, /// with a message and a data object to include in the response. /// /// The status code to set on the response. /// A message to include in the response as plain text. /// The data object to include in the response. public HttpException(HttpStatusCode statusCode, string? message, object? data) : this((int)statusCode, message, data) { } /// public int StatusCode { get; } /// public object? DataObject { get; } /// string? IHttpException.Message => HttpExceptionMessage; // This property is necessary because when an exception with a null Message is thrown // the CLR provides a standard message. We want null to remain null in IHttpException. private string? HttpExceptionMessage { get; } /// /// /// This method does nothing; there is no need to call /// base.PrepareResponse in overrides of this method. /// public virtual void PrepareResponse(IHttpContext context) { } } }