using System;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Web;
namespace WebDAVSharp.Server.Exceptions
{
///
/// This exception, or a descendant, is thrown when requests fail, specifying the status code
/// that the server should return back to the client.
///
[Serializable]
public class WebDavException : HttpException
{
private const string StatusCodeKey = "StatusCode";
private const string StatusDescriptionKey = "StatusDescription";
///
/// Initializes a new instance of the class.
///
public WebDavException()
: this(HttpStatusCode.InternalServerError)
{
// Do nothing here
}
///
/// Initializes a new instance of the class.
///
/// The exception message stating the reason for the exception being thrown.
public WebDavException(string message)
: this(HttpStatusCode.InternalServerError, message)
{
// Do nothing here
}
///
/// Initializes a new instance of the class.
///
/// The exception message stating the reason for the exception being thrown.
///
/// The
/// that is the cause for this exception;
/// or
/// null if no inner exception is specified.
///
public WebDavException(string message, Exception innerException)
: this(HttpStatusCode.InternalServerError, message, innerException)
{
// Do nothing here
}
///
/// 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.
///
/// The parameter is null.
///
/// The class name is null or
/// is zero (0).
///
protected WebDavException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
StatusCode = info.GetInt32(StatusCodeKey);
StatusDescription = info.GetString(StatusDescriptionKey);
}
///
/// Initializes a new instance of the class.
///
/// The HTTP status code that this maps to.
/// The exception message stating the reason for the exception being thrown.
///
/// The
/// that is the cause for this exception;
/// or
/// null if no inner exception is specified.
///
public WebDavException(HttpStatusCode statusCode, string message = null, Exception innerException = null)
: base(GetMessage(statusCode, message), innerException)
{
StatusCode = (int) statusCode;
StatusDescription = HttpWorkerRequest.GetStatusDescription((int) statusCode);
}
///
/// Gets the HTTP status code that this maps to.
///
///
/// The status code.
///
public int StatusCode { get; }
///
/// Gets the status description for the HTTP .
///
///
/// The status description.
///
public string StatusDescription { get; }
///
/// 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.
///
///
/// The parameter is a null reference (Nothing in
/// Visual Basic).
///
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(StatusCodeKey, StatusCode);
info.AddValue(StatusDescriptionKey, StatusDescription);
}
///
/// Gets the message.
///
/// The status code.
/// The message.
/// The message and the status description.
private static string GetMessage(HttpStatusCode statusCode, string message)
{
string format = "%s";
if (!String.IsNullOrWhiteSpace(message))
format = message;
return format.Replace("%s", HttpWorkerRequest.GetStatusDescription((int) statusCode));
}
}
}