WebDavException.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Net;
  3. using System.Runtime.Serialization;
  4. using System.Security.Permissions;
  5. using System.Web;
  6. namespace WebDAVSharp.Server.Exceptions
  7. {
  8. /// <summary>
  9. /// This exception, or a descendant, is thrown when requests fail, specifying the status code
  10. /// that the server should return back to the client.
  11. /// </summary>
  12. [Serializable]
  13. public class WebDavException : HttpException
  14. {
  15. private const string StatusCodeKey = "StatusCode";
  16. private const string StatusDescriptionKey = "StatusDescription";
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="WebDavException" /> class.
  19. /// </summary>
  20. public WebDavException()
  21. : this(HttpStatusCode.InternalServerError)
  22. {
  23. // Do nothing here
  24. }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="WebDavException" /> class.
  27. /// </summary>
  28. /// <param name="message">The exception message stating the reason for the exception being thrown.</param>
  29. public WebDavException(string message)
  30. : this(HttpStatusCode.InternalServerError, message)
  31. {
  32. // Do nothing here
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="WebDavException" /> class.
  36. /// </summary>
  37. /// <param name="message">The exception message stating the reason for the exception being thrown.</param>
  38. /// <param name="innerException">
  39. /// The
  40. /// <see cref="Exception" /> that is the cause for this exception;
  41. /// or
  42. /// <c>null</c> if no inner exception is specified.
  43. /// </param>
  44. public WebDavException(string message, Exception innerException)
  45. : this(HttpStatusCode.InternalServerError, message, innerException)
  46. {
  47. // Do nothing here
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="WebDavException" /> class.
  51. /// </summary>
  52. /// <param name="info">
  53. /// The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object
  54. /// data about the exception being thrown.
  55. /// </param>
  56. /// <param name="context">
  57. /// The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual
  58. /// information about the source or destination.
  59. /// </param>
  60. /// <exception cref="T:System.ArgumentNullException">The <paramref name="info" /> parameter is null.</exception>
  61. /// <exception cref="T:System.Runtime.Serialization.SerializationException">
  62. /// The class name is null or
  63. /// <see cref="P:System.Exception.HResult" /> is zero (0).
  64. /// </exception>
  65. protected WebDavException(SerializationInfo info, StreamingContext context)
  66. : base(info, context)
  67. {
  68. StatusCode = info.GetInt32(StatusCodeKey);
  69. StatusDescription = info.GetString(StatusDescriptionKey);
  70. }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="WebDavException" /> class.
  73. /// </summary>
  74. /// <param name="statusCode">The HTTP status code that this <see cref="WebDavException" /> maps to.</param>
  75. /// <param name="message">The exception message stating the reason for the exception being thrown.</param>
  76. /// <param name="innerException">
  77. /// The
  78. /// <see cref="Exception" /> that is the cause for this exception;
  79. /// or
  80. /// <c>null</c> if no inner exception is specified.
  81. /// </param>
  82. public WebDavException(HttpStatusCode statusCode, string message = null, Exception innerException = null)
  83. : base(GetMessage(statusCode, message), innerException)
  84. {
  85. StatusCode = (int) statusCode;
  86. StatusDescription = HttpWorkerRequest.GetStatusDescription((int) statusCode);
  87. }
  88. /// <summary>
  89. /// Gets the HTTP status code that this <see cref="WebDavException" /> maps to.
  90. /// </summary>
  91. /// <value>
  92. /// The status code.
  93. /// </value>
  94. public int StatusCode { get; }
  95. /// <summary>
  96. /// Gets the status description for the HTTP <see cref="StatusCode" />.
  97. /// </summary>
  98. /// <value>
  99. /// The status description.
  100. /// </value>
  101. public string StatusDescription { get; }
  102. /// <summary>
  103. /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with
  104. /// information about the exception.
  105. /// </summary>
  106. /// <param name="info">
  107. /// The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object
  108. /// data about the exception being thrown.
  109. /// </param>
  110. /// <param name="context">
  111. /// The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual
  112. /// information about the source or destination.
  113. /// </param>
  114. /// <exception cref="T:System.ArgumentNullException">
  115. /// The <paramref name="info" /> parameter is a null reference (Nothing in
  116. /// Visual Basic).
  117. /// </exception>
  118. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  119. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  120. {
  121. base.GetObjectData(info, context);
  122. info.AddValue(StatusCodeKey, StatusCode);
  123. info.AddValue(StatusDescriptionKey, StatusDescription);
  124. }
  125. /// <summary>
  126. /// Gets the message.
  127. /// </summary>
  128. /// <param name="statusCode">The status code.</param>
  129. /// <param name="message">The message.</param>
  130. /// <returns>The message and the status description.</returns>
  131. private static string GetMessage(HttpStatusCode statusCode, string message)
  132. {
  133. string format = "%s";
  134. if (!String.IsNullOrWhiteSpace(message))
  135. format = message;
  136. return format.Replace("%s", HttpWorkerRequest.GetStatusDescription((int) statusCode));
  137. }
  138. }
  139. }