Пример #1
0
        /// <summary>
        /// Gets the exception to be utilized in handling a caught exception,
        /// based on the specified policy.
        /// </summary>
        /// <param name="exception">
        /// The caught exception, requiring handling.
        /// </param>
        /// <param name="policy">
        /// The policy guiding the approach to be taken in handling the exception.
        /// </param>
        /// <returns>
        /// The <see cref="Exception"/> to be utilized in handling a caught exception.
        /// </returns>
        private static Exception GetException(Exception exception, string policy)
        {
            ExceptionHandlingApproach approach = ExceptionHandlingApproach.Rethrow;
            Type type = null;

            // TODO: implement configuration-based policy management
            // HACK: setup dummy type, in case approach is set to Swap; remove when configuration is implemented
            type = exception.GetType();

            // apply exception management policy
            return(ExceptionManager.ApplyExceptionManagementPolicySettings(
                       exception, approach, type));
        }
Пример #2
0
        /// <summary>
        /// Applies the exception management policy specified by the Exception Handling
        /// Approach and the related Exception type.
        /// </summary>
        /// <param name="exception">
        /// The exception requiring management.
        /// </param>
        /// <param name="approach">
        /// The approach to be taken in handling the exception.
        /// </param>
        /// <param name="exceptionType">
        /// The <see cref="Type"/> of the Exception defined in the policy related
        /// to <paramref name="approach"/>.
        /// </param>
        /// <returns>
        /// The <see cref="Exception"/> to be utilized in handling a caught exception.
        /// </returns>
        private static Exception ApplyExceptionManagementPolicySettings(
            Exception exception, ExceptionHandlingApproach approach, Type exceptionType)
        {
            Exception returnException = null;

            #region // apply policy

            // check and apply the configured approach
            switch (approach)
            {
            case ExceptionHandlingApproach.Sink:
                // return a null reference, forcing the exception to be sunk
                returnException = null;

                break;

            case ExceptionHandlingApproach.Wrap:
                try
                {
                    // create container exception, with caught exception wrapped
                    returnException = (Exception)ObjectFactory.CreateObject(exceptionType,
                                                                            exception.Message, exception);
                }
                catch
                {
                    // return the same exception
                    returnException = exception;

                    // sink exception
                }

                break;

            case ExceptionHandlingApproach.Swap:
                try
                {
                    // create container exception, swapping out original exception
                    // however, the message from the original exception is retained
                    returnException = (Exception)ObjectFactory.CreateObject(exceptionType,
                                                                            exception.Message);
                }
                catch
                {
                    // return the same exception
                    returnException = exception;

                    // sink exception
                }

                break;

            case ExceptionHandlingApproach.Rethrow:
            default:
                // return the same exception
                returnException = exception;

                break;
            }

            #endregion // apply policy

            return(returnException);
        }