/// <summary> /// Lanza la nueva excepción que remplaza a <paramref name="exception"/> /// </summary> /// <param name="exception">Excepción a remplazar</param> /// <param name="exceptionPolicyConf">Incormación de configuración que contiene el tipo /// de la nueva excepción</param> private static void ReplaceException(Exception exception, ExceptionPolicyConfiguration exceptionPolicyConf) { Exception ex = Activator.CreateInstance(exceptionPolicyConf.NewExceptionType, new object[] { exceptionPolicyConf.NewExceptionMessage }) as Exception; if (ex == null) { throw new ExceptionHandlingException(Messages.TypeDoesNotInheritsFromException + ExceptionTypeElement.GetTypeName(exceptionPolicyConf.NewExceptionType)); } throw ex; }
/// <summary>{+ /// /// Envuelve a <paramref name="originalEx"/> en una nueva excepción definida en /// <paramref name="exceptionPolicyConf"/> /// </summary> /// <param name="originalEx">Excepción original</param> /// <param name="exceptionPolicyConf">Información de configuración</param> private static void WrapException(Exception originalEx, ExceptionPolicyConfiguration exceptionPolicyConf) { Exception ex = Activator.CreateInstance(exceptionPolicyConf.NewExceptionType, new object[] { exceptionPolicyConf.NewExceptionMessage, originalEx }) as Exception; if (ex == null) { throw new ExceptionHandlingException(Messages.TypeDoesNotInheritsFromException + ExceptionTypeElement.GetTypeName(exceptionPolicyConf.NewExceptionType)); } // Lanzo la nueva excepción throw ex; }
/// <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) { //Application wide cases: 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); } throw exception; 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; case HandlingAction.JustLog: LogException(exception); break; default: throw new ExceptionHandlingException(exception, Messages.InvalidAction); } return(exceptionPolicy.Action == HandlingAction.Rethrow || exceptionPolicy.Action == HandlingAction.LogAndRethrow); }