RaspberryIO/Unosquare.Swan/Exceptions/LdapException.cs
2019-12-03 18:44:25 +01:00

133 lines
5.2 KiB
C#

using System;
using Unosquare.Swan.Networking.Ldap;
namespace Unosquare.Swan.Exceptions {
/// <summary>
/// Thrown to indicate that an Ldap exception has occurred. This is a general
/// exception which includes a message and an Ldap result code.
/// An LdapException can result from physical problems (such as
/// network errors) as well as problems with Ldap operations detected
/// by the server. For example, if an Ldap add operation fails because of a
/// duplicate entry, the server returns a result code.
/// </summary>
/// <seealso cref="System.Exception" />
public class LdapException
: Exception {
internal const String UnexpectedEnd = "Unexpected end of filter";
internal const String MissingLeftParen = "Unmatched parentheses, left parenthesis missing";
internal const String MissingRightParen = "Unmatched parentheses, right parenthesis missing";
internal const String ExpectingRightParen = "Expecting right parenthesis, found \"{0}\"";
internal const String ExpectingLeftParen = "Expecting left parenthesis, found \"{0}\"";
private readonly String _serverMessage;
/// <summary>
/// Initializes a new instance of the <see cref="LdapException" /> class.
/// Constructs an exception with a detailed message obtained from the
/// specified <c>MessageOrKey</c> String.
/// Additional parameters specify the result code, the message returned
/// from the server, and a matchedDN returned from the server.
/// The String is used either as a message key to obtain a localized
/// message from ExceptionMessages, or if there is no key in the
/// resource matching the text, it is used as the detailed message itself.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="resultCode">The result code returned.</param>
/// <param name="serverMsg">Error message specifying additional information
/// from the server.</param>
/// <param name="matchedDN">The maximal subset of a specified DN which could
/// be matched by the server on a search operation.</param>
/// <param name="rootException">The root exception.</param>
public LdapException(
String message,
LdapStatusCode resultCode,
String serverMsg = null,
String matchedDN = null,
Exception rootException = null)
: base(message) {
this.ResultCode = resultCode;
this.Cause = rootException;
this.MatchedDN = matchedDN;
this._serverMessage = serverMsg;
}
/// <summary>
/// Returns the error message from the Ldap server, if this message is
/// available (that is, if this message was set). If the message was not set,
/// this method returns null.
/// </summary>
/// <returns>
/// The error message or null if the message was not set.
/// </returns>
public String LdapErrorMessage =>
this._serverMessage != null && this._serverMessage.Length == 0 ? null : this._serverMessage;
/// <summary>
/// Returns the lower level Exception which caused the failure, if any.
/// For example, an IOException with additional information may be returned
/// on a CONNECT_ERROR failure.
/// </summary>
/// <value>
/// The cause.
/// </value>
public Exception Cause {
get;
}
/// <summary>
/// Returns the result code from the exception.
/// The codes are defined as <c>public final static int</c> members
/// of the Ldap Exception class. If the exception is a
/// result of error information returned from a directory operation, the
/// code will be one of those defined for the class. Otherwise, a local error
/// code is returned.
/// </summary>
/// <value>
/// The result code.
/// </value>
public LdapStatusCode ResultCode {
get;
}
/// <summary>
/// Returns the part of a submitted distinguished name which could be
/// matched by the server.
/// If the exception was caused by a local error, such as no server
/// available, the return value is null. If the exception resulted from
/// an operation being executed on a server, the value is an empty string
/// except when the result of the operation was one of the following:.
/// <ul><li>NO_SUCH_OBJECT</li><li>ALIAS_PROBLEM</li><li>INVALID_DN_SYNTAX</li><li>ALIAS_DEREFERENCING_PROBLEM</li></ul>
/// </summary>
/// <value>
/// The matched dn.
/// </value>
public String MatchedDN {
get;
}
/// <inheritdoc />
public override String Message => this.ResultCode.ToString().Humanize();
/// <inheritdoc />
public override String ToString() {
// Craft a string from the resource file
String msg = $"{nameof(LdapException)}: {base.Message} ({this.ResultCode}) {this.ResultCode.ToString().Humanize()}";
// Add server message
if(!String.IsNullOrEmpty(this._serverMessage)) {
msg += $"\r\nServer Message: {this._serverMessage}";
}
// Add Matched DN message
if(this.MatchedDN != null) {
msg += $"\r\nMatched DN: {this.MatchedDN}";
}
if(this.Cause != null) {
msg += $"\r\n{this.Cause}";
}
return msg;
}
}
}