// // FolderNotOpenException.cs // // Author: Jeffrey Stedfast // // Copyright (c) 2013-2020 .NET Foundation and Contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; #if SERIALIZABLE using System.Runtime.Serialization; #endif namespace MailKit { /// /// The exception that is thrown when a folder is not open. /// /// /// This exception is thrown when an operation on a folder could not be completed /// due to the folder being in a closed state. For example, the /// /// method will throw a if the folder is not /// current open. /// #if SERIALIZABLE [Serializable] #endif public class FolderNotOpenException : InvalidOperationException { #if SERIALIZABLE /// /// Initializes a new instance of the class. /// /// /// Deserializes a . /// /// The serialization info. /// The streaming context. /// /// is null. /// protected FolderNotOpenException (SerializationInfo info, StreamingContext context) : base (info, context) { var value = info.GetString ("FolderAccess"); FolderAccess access; if (!Enum.TryParse (value, out access)) FolderAccess = FolderAccess.ReadOnly; else FolderAccess = access; FolderName = info.GetString ("FolderName"); } #endif /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The folder name. /// The minimum folder access required by the operation. /// The error message. /// The inner exception. /// /// is null. /// public FolderNotOpenException (string folderName, FolderAccess access, string message, Exception innerException) : base (message, innerException) { if (folderName == null) throw new ArgumentNullException (nameof (folderName)); FolderName = folderName; FolderAccess = access; } /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The folder name. /// The minimum folder access required by the operation. /// The error message. /// /// is null. /// public FolderNotOpenException (string folderName, FolderAccess access, string message) : base (message) { if (folderName == null) throw new ArgumentNullException (nameof (folderName)); FolderName = folderName; FolderAccess = access; } /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// /// The folder name. /// The minimum folder access required by the operation. /// /// is null. /// public FolderNotOpenException (string folderName, FolderAccess access) : this (folderName, access, GetDefaultMessage (access)) { } /// /// Get the name of the folder. /// /// /// Gets the name of the folder. /// /// The name of the folder. public string FolderName { get; private set; } /// /// Get the minimum folder access required by the operation. /// /// /// Gets the minimum folder access required by the operation. /// /// The minimum required folder access. public FolderAccess FolderAccess { get; private set; } static string GetDefaultMessage (FolderAccess access) { if (access == FolderAccess.ReadWrite) return "The folder is not currently open in read-write mode."; return "The folder is not currently open."; } #if SERIALIZABLE /// /// When overridden in a derived class, sets the /// with information about the exception. /// /// /// Serializes the state of the . /// /// The serialization info. /// The streaming context. /// /// is null. /// public override void GetObjectData (SerializationInfo info, StreamingContext context) { base.GetObjectData (info, context); info.AddValue ("FolderAccess", FolderAccess.ToString ()); info.AddValue ("FolderName", FolderName); } #endif } }