using System; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using CMS.DataCom; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.UIControls; /// /// A control that displays error messages and details about Data.com REST API errors. /// public partial class CMSModules_ContactManagement_Controls_UI_DataCom_ErrorSummary : CMSAdminControl { #region "Variables" /// /// The error message to display. /// private new string mErrorMessage; /// /// The exception to display if development mode is enabled. /// private Exception mException; /// /// The list of Data.com REST API errors to display. /// private Error[] mErrors; #endregion #region "Properties" /// /// Gets or sets the value indicating whether this control will show errors using the page messages. /// public bool MessagesEnabled { get { object enabled = ViewState["MessagesEnabled"]; return enabled == null ? false : (bool)enabled; } set { ViewState["MessagesEnabled"] = value; } } #endregion #region "Public methods" /// /// Reports an error. /// /// The error message to report. public void Report(string errorMessage) { mErrorMessage = errorMessage; DisplayError(); } /// /// Reports an error and related exception. /// /// The error message to report. /// The exception to report. public void Report(string errorMessage, Exception exception) { mErrorMessage = errorMessage; mException = exception; DisplayError(); } /// /// Reports the specified exception. /// /// The exception to report. public void Report(Exception exception) { mException = exception; mErrorMessage = GetErrorMessageForException(exception); DataComException customException = exception as DataComException; if (customException != null) { mErrorMessage = GetString("datacom.errormessage"); mErrors = customException.Errors.ToArray(); } DisplayError(); } #endregion #region "Private methods" /// /// Displays an error using the provided information. /// private void DisplayError() { LocalizeErrors(); string message = GetErrorMessage(); string description = GetErrorDescription(); string tooltip = GetErrorTooltip(); if (MessagesEnabled) { ShowError(message, description, tooltip); } else { HtmlGenericControl paragraph = new HtmlGenericControl("p") { InnerHtml = message }; paragraph.Attributes.Add("class", "Red"); if (!String.IsNullOrEmpty(tooltip)) { paragraph.InnerHtml = String.Format("{0}
{1}", paragraph.InnerHtml, tooltip); } Controls.Add(paragraph); if (!String.IsNullOrEmpty(description)) { Literal literal = new Literal { Text = description }; Controls.Add(literal); } } } /// /// Formats the error message using the provided information, and returns it. /// /// An error message. private string GetErrorMessage() { return HTMLHelper.HTMLEncode(mErrorMessage); } /// /// Formats the error description using the provided information, and returns it. /// /// An error description. private string GetErrorDescription() { if (mErrors != null && mErrors.Length > 0) { StringBuilder builder = new StringBuilder(); builder.Append(""); return builder.ToString(); } return String.Empty; } /// /// Formats the error tooltip using the provided information, and returns it. /// /// An error tooltip. private string GetErrorTooltip() { if (SettingsKeyProvider.DevelopmentMode && mException != null) { return HTMLHelper.HTMLEncode(mException.Message ?? mException.ToString()); } return String.Empty; } /// /// Creates an error message for the specified exception, and returns it. /// /// The exception to handle. /// An error message for the specified exception. private string GetErrorMessageForException(Exception exception) { string resourceName = "datacom.exception"; if (exception is WebException || (exception.InnerException != null && exception.InnerException is WebException)) { resourceName = "datacom.serviceexception"; } return GetString(resourceName); } /// /// Localizes Data.com errors into current UI language, if applicable. /// private void LocalizeErrors() { if (mErrors != null) { foreach (Error error in mErrors.Where(x => !String.IsNullOrEmpty(x.ErrorCode))) { string name = String.Format("datacom.error.{0}", error.ErrorCode); string message = ResHelper.GetString(name, Thread.CurrentThread.CurrentUICulture.Name, String.Empty); if (!String.IsNullOrEmpty(message)) { error.ErrorMessage = message; } } } } #endregion }