Sunday, November 16, 2014

Method to Process Exception


        /// <summary>
        ///
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static string ProcessException(Exception exception)
        {
            // Gets or sets exception mode;stored in web.config;Admin/User
            string exceptionMode = ConfigurationManager.AppSettings[ERPUtilityResources.ExceptionMode];
            // Gets or sets exception details
            string[] exceptionDetails = exception.Message.Split(ERPUtilityResources.ExceptionToken.ToCharArray());
            // Gets or sets exception source;where from exception occured
            string exceptionSource = exceptionDetails[0];
            // Gets or sets exception details count;client exception have only 1 details
            int count = exceptionDetails.Count();
            // Gets or sets error message
            string errorMessage = String.Empty;
            // Gets or sets page name
            string pageName = String.Empty;
            // Gets or sets field name (eg.pageName fieldName already exists)
            string fieldName = String.Empty;
            // Checking exception details ends with GTI_EXCEPTION;then it comes from service side
            if (exceptionDetails[count - 1].Equals(ERPUtilityResources.GTIException))
            {
                // Gets or sets exception key;key is stored as count-4 to count-3 position in exception message
                string exceptionKey = exceptionDetails[count - 4] + ERPUtilityResources.ExceptionToken + exceptionDetails[count - 3];
                // Checking exception mode is admin
                if (exceptionMode.Equals(ERPUtilityResources.Admin))
                {
                    // Gets exception message from exception details;replacing GTI_EXCEPTION_DETAILS token from message
                    errorMessage = exceptionDetails[count - 2].Replace(ERPUtilityResources.GTIExceptionDetails, String.Empty);
                    // Checking no exception details found
                    if (errorMessage.Equals(String.Empty))
                    {
                        // Sets exception message to unknown
                        errorMessage = ERPUtilityResources.UnHandledException;
                    }
                    // Return exception message to client;by encoding it;shows using jquery message
                    return errorMessage.Replace(ERPUtilityResources.SingleQuoteCharacter, ERPUtilityResources.SpaceCharacter).Replace(ERPUtilityResources.DoubleQuoteCharacter, ERPUtilityResources.SpaceCharacter).Replace(ERPUtilityResources.EnterCharacter, ERPUtilityResources.BreakTag).Replace(ERPUtilityResources.CommaCharacter, ERPUtilityResources.BreakTag).Replace(ERPUtilityResources.EditEditConcurrencyException, ERPUtilityResources.Msg_Save_Error_Concurrent);
                }
                else
                {
                    // Gets error list hash table;stored in applicaton
                    Hashtable errorList = (Hashtable)HttpContext.Current.Application[ERPUtilityResources.ErrorTable];
                    // Gets  page list hash table;stored in applicaton
                    Hashtable pageList = (Hashtable)HttpContext.Current.Application[ERPUtilityResources.PageTable];
                    // Gets field list hash table;stored in applicaton
                    Hashtable fieldList = (Hashtable)HttpContext.Current.Application[ERPUtilityResources.FieldTable];
                    // Gets pageName from application using exception source as key in error hash table
                    pageName = (pageList == null || pageList[exceptionSource] == null) ? String.Empty : pageList[exceptionSource].ToString();
                    // Gets fieldName from application using exception source as key in field hash table
                    fieldName = (fieldList == null || fieldList[exceptionSource] == null) ? "Code" : fieldList[exceptionSource].ToString();
                    // Constucting error message by appending  page name + field name + error message
                    errorMessage = String.Format((errorList == null || errorList[exceptionKey] == null) ? String.Empty : errorList[exceptionKey].ToString(),
                                pageName, fieldName);
                    // Checking no error message is empty
                    if (errorMessage.Equals(String.Empty))
                    {
                        // Sets error message as unhandled exception
                        errorMessage = ERPUtilityResources.UnHandledException;
                    }
                    // Return exception message to client;by encoding it;shows using jquery message
                    return errorMessage.Replace(ERPUtilityResources.EnterCharacter, ERPUtilityResources.BreakTag).Replace(ERPUtilityResources.CommaCharacter, ERPUtilityResources.BreakTag);
                }
            }
            else
            {
                // Return client exception message to client;by encoding it;shows using jquery message
                return exception.Message.Replace(ERPUtilityResources.SingleQuoteCharacter, ERPUtilityResources.SpaceCharacter).Replace(ERPUtilityResources.DoubleQuoteCharacter, ERPUtilityResources.SpaceCharacter).Replace(ERPUtilityResources.EnterCharacter, ERPUtilityResources.BreakTag).Replace(ERPUtilityResources.CommaCharacter, ERPUtilityResources.BreakTag);
            }
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...