Ejemplo n.º 1
0
        /// <summary>
        /// Obtiene la configuración de la política para una excepción del tipo de
        /// <paramref name="exception"/>
        /// </summary>
        /// <param name="exception">Excepción para la que se quieren los valore de configuración
        /// </param>
        /// <param name="policyConf">Valores de configuración de una política</param>
        /// <returns>Valores de configuración para <paramref name="exception"/></returns>
        private static ExceptionPolicyConfiguration GetExceptionPolicyConfiguration(
            Exception exception, PolicyConfiguration policyConf)
        {
            if (policyConf == null)
            {
                throw new ExceptionHandlingException(exception, string.Format(Messages.ParameterCanBeNull, "policyConf"));
            }
            if (exception == null)
            {
                throw new ExceptionHandlingException(exception, string.Format(Messages.ParameterCanBeNull, "exception"));
            }

            ExceptionPolicyConfiguration exPolicyConf = null;

            Type exceptionType = exception.GetType();

            while (exceptionType != typeof(object))
            {
                exPolicyConf = policyConf.ExceptionPolicyList.Find(
                    pe => pe.ExceptionType == exceptionType);

                if (exPolicyConf == null)
                {
                    exceptionType = exceptionType.BaseType;
                }
                else
                {
                    break;
                    // encontré la exceptionPolicyConfiguration asociada al parámetro exception
                }
            }

            return(exPolicyConf);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Carga la configuración a partir de <paramref name="section"/>
        /// </summary>
        /// <param name="section">Sección de configuración del archivo de configuración</param>
        private static void LoadConfigurationFromConfigurationSection(
            ExceptionHandlingSection section)
        {
            foreach (ExceptionPolicyElement policyElement in section.PolicyCollection)
            {
                PolicyConfiguration policyConf = new PolicyConfiguration()
                {
                    Name = policyElement.Name,
                    Log  = policyElement.IsLogged,
                    ExceptionPolicyList = new List <ExceptionPolicyConfiguration>()
                };

                foreach (ExceptionTypeElement exTypeElement in policyElement.ExceptionTypeCollection)
                {
                    ExceptionPolicyConfiguration politicaExcepcion = new ExceptionPolicyConfiguration()
                    {
                        Name   = exTypeElement.Name,
                        Action = exTypeElement.HandlingAction,
                        NewExceptionMessage = exTypeElement.NewExceptionMessage,
                        ExceptionType       = exTypeElement.ExceptionType,
                        NewExceptionType    = exTypeElement.NewExceptionType
                    };

                    policyConf.ExceptionPolicyList.Add(politicaExcepcion);
                }

                ExceptionPolicy.policyConfigurationList.Add(policyConf);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Obtiene el <see cref="PolicyConfiguration"/> de la lista
        /// </summary>
        /// <param name="policyName">Nombre de la política a obtener</param>
        /// <returns>Política obtenida</returns>
        private static PolicyConfiguration GetPolicyConfiguration(string policyName)
        {
            PolicyConfiguration policyConf = policyConfigurationList.Find(
                cp => cp.Name == policyName);

            return(policyConf);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Carga la configuración a partir de los valores por defecto
        /// </summary>
        private static void LoadConfigurationFromDefaultValues()
        {
            string defaultExceptionTypeName = "Exception";
            bool   defaultPolicyLog         = true;

            PolicyConfiguration policyConf = new PolicyConfiguration()
            {
                Name = Defaults.DefaultExceptionPolicy,
                Log  = defaultPolicyLog,
                ExceptionPolicyList = new List <ExceptionPolicyConfiguration>()
            };

            policyConf.ExceptionPolicyList.Add(new ExceptionPolicyConfiguration()
            {
                Name          = defaultExceptionTypeName,
                Action        = HandlingAction.Rethrow,
                ExceptionType = typeof(Exception)
            });

            ExceptionPolicy.policyConfigurationList.Add(policyConf);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Punto de entrada principal del módulo de manejo de excepciones.
        /// </summary>
        /// <param name="exception">El objeto de <see cref="Exception"/> a manejar</param>
        /// <param name="policyName">El nombre de la política con que se quiere manejar la excepción</param>
        /// <returns>Si se recomienda relanzar o no</returns>
        /// <example>
        /// El siguiente código muestra un ejemplo del uso del componente
        /// <code>
        /// try
        ///	{
        ///		Foo();
        ///	}
        ///	catch (Exception e)
        ///	{
        ///		if (ExceptionPolicy.HandleException(e, name)) throw;
        ///	}
        /// </code>
        /// </example>
        public static bool HandleException(Exception exception, string policyName)
        {
            PolicyConfiguration policyConf = GetPolicyConfiguration(policyName);

            if (policyConf == null)
            {
                throw new ExceptionHandlingException(exception, Messages.InexistentPolicyName + policyName);
            }

            ExceptionPolicyConfiguration exceptionPolicy = GetExceptionPolicyConfiguration(exception, policyConf);

            if (exceptionPolicy == null)
            {
                throw new ExceptionHandlingException(exception, string.Format(Messages.PolicyWithoutAssociatedType,
                                                                              policyName, ExceptionTypeElement.GetTypeName(exception.GetType())));
            }

            //RAP.29/04/2013. Si la política tiene el log habilitado convierto la acción a la correspondiente de log
            switch (exceptionPolicy.Action)
            {
            case HandlingAction.None:
                break;

            case HandlingAction.Rethrow:
                if (policyConf.Log)
                {
                    LogException(exception);
                }

                break;

            case HandlingAction.Wrap:
                if (policyConf.Log)
                {
                    LogException(exception);
                }

                WrapException(exception, exceptionPolicy);
                break;

            case HandlingAction.Replace:
                if (policyConf.Log)
                {
                    LogException(exception);
                }

                ReplaceException(exception, exceptionPolicy);
                break;

            case HandlingAction.LogAndRethrow:
                LogException(exception);
                break;

            case HandlingAction.LogAndWrap:
                LogException(exception);
                WrapException(exception, exceptionPolicy);
                break;

            case HandlingAction.LogAndReplace:
                LogException(exception);
                ReplaceException(exception, exceptionPolicy);
                break;

            default:
                throw new ExceptionHandlingException(exception, Messages.InvalidAction);
            }

            return(exceptionPolicy.Action == HandlingAction.Rethrow || exceptionPolicy.Action == HandlingAction.LogAndRethrow);
        }