예제 #1
0
        /// <summary>
        /// Wraps the given exception into a SelfDocumentingException instance after adding the instance variables,
        /// method parameters and local variables to it
        /// </summary>
        /// <param name="e">The exception to wrap.</param>
        /// <param name="message">The message for the SelfDocumentingException</param>
        /// <param name="methodName">The fully qualified method name from where the exception is thrown.</param>
        /// <param name="instanceVarsNames">The names of the instance variables.</param>
        /// <param name="instanceVars">The values of the instance variables at time of exception.</param>
        /// <param name="parameterVarsNames">The names of the method parameters.</param>
        /// <param name="parameterVars">The values of the method parameters at time of exception.</param>
        /// <param name="localVarsNames">The names of the local variables.</param>
        /// <param name="localVars">The values of the local variables at time of exception.</param>
        /// <returns>The formed SelfDocumentingException instance.</returns>
        internal static SelfDocumentingException GetSelfDocumentingException(
            Exception e, string message, string methodName, string[] instanceVarsNames, object[] instanceVars,
            string[] parameterVarsNames, object[] parameterVars, string[] localVarsNames, object[] localVars)
        {
            //Wrap only if it is not already of type SelfDocumentingException
            SelfDocumentingException sde = null;

            if (e is SelfDocumentingException)
            {
                sde = (SelfDocumentingException)e;
            }
            else
            {
                sde = new SelfDocumentingException(message, e);
            }

            MethodState ms = sde.PinMethod(methodName, e.StackTrace);

            //Add instance variables, method parameters and local variables
            for (int i = 0; i < instanceVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddInstanceVariable(instanceVarsNames[i], instanceVars[i]);
                }
                catch
                {
                }
            }
            for (int i = 0; i < parameterVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddMethodParameter(parameterVarsNames[i], parameterVars[i]);
                }
                catch
                {
                }
            }
            for (int i = 0; i < localVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddLocalVariable(localVarsNames[i], localVars[i]);
                }
                catch
                {
                }
            }

            ms.Lock();
            return(sde);
        }
예제 #2
0
파일: HelperTests.cs 프로젝트: kurtrips/tc
        public void TestGetSelfDocumentingException()
        {
            Exception e = new Exception("Message");

            SelfDocumentingException sde = Helper.GetSelfDocumentingException(
                e, "My Mesasge", "Cold.Turkey", new string[0], new object[0],
                new string[0], new object[0], new string[0], new object[0]);

            Assert.AreEqual(sde.InnerException, e, "Inner exception is incorrect.");
        }
예제 #3
0
        /// <summary>
        /// <para>
        /// Wraps the given exception into a SDE.
        /// </para>
        /// </summary>
        ///
        /// <remarks>
        /// <para>
        /// This method only be invoked in <see cref="GetClient(string)"/>.
        /// </para>
        /// </remarks>
        /// <param name="e">
        /// <see cref="Exception"/> instance.
        /// </param>
        /// <param name="namespace">
        /// namespace string.
        /// </param>
        /// <param name="message">
        /// Error message.
        /// </param>
        /// <returns>
        /// <see cref="SelfDocumentingException"/> instance.
        /// </returns>
        private static Exception WrapSDE(Exception e, string @namespace,
                                         string message)
        {
            SelfDocumentingException sde = e as SelfDocumentingException;

            if (sde == null)
            {
                sde = new SelfDocumentingException(message, e);
            }
            MethodState methodState = sde.PinMethod(String.Format("{0}.{1}",
                                                                  typeof(HermesAuthorizationServiceClient).FullName,
                                                                  "GetClient"), e.StackTrace);

            methodState.AddMethodParameter("namespace", @namespace);
            methodState.Lock();
            return(sde);
        }
예제 #4
0
        /// <summary>
        /// <para>
        /// Constructs a <see cref="SelfDocumentingException"/> instance with all related data.
        /// </para>
        /// </summary>
        ///
        /// <param name="sde">
        /// The <see cref="SelfDocumentingException"/> instance.
        /// </param>
        /// <param name="instanceNames">
        /// the instance variable names.
        /// </param>
        /// <param name="instanceValues">
        /// the instance variable values.
        /// </param>
        /// <param name="paramNames">
        /// The parameter variable names.
        /// </param>
        /// <param name="paramValues">
        /// The parameter variable values.
        /// </param>
        /// <param name="localNames">
        /// The local variable names.
        /// </param>
        /// <param name="localValues">
        /// The local variable values.
        /// </param>
        /// <param name="stackFrameIndex">
        /// The index to the stack frame where the method base will be
        /// obtained from the stack trace.
        /// </param>
        ///
        /// <returns>
        /// The <see cref="SelfDocumentingException"/> instance.
        /// </returns>
        internal static SelfDocumentingException ConstructSDE(
            SelfDocumentingException sde,
            string[] instanceNames, object[] instanceValues,
            string[] paramNames, object[] paramValues,
            string[] localNames, object[] localValues,
            int stackFrameIndex)
        {
            // Pin the method. An index is used to determine which particular stack frame to get the method from.
            MethodBase methodBase = new StackTrace().GetFrame(stackFrameIndex).GetMethod();

            MethodState ms = sde.PinMethod(methodBase.DeclaringType.FullName + "." + methodBase.Name,
                                           ((sde.InnerException == null) ? sde : sde.InnerException).StackTrace);

            // Add instance variables
            if (instanceNames != null)
            {
                for (int i = 0; ((i < instanceNames.Length) && (i < instanceValues.Length)); i++)
                {
                    ms.AddInstanceVariable(instanceNames[i], instanceValues[i]);
                }
            }

            // Add parameter variables
            if (paramNames != null)
            {
                for (int i = 0; ((i < paramNames.Length) && (i < paramValues.Length)); i++)
                {
                    ms.AddMethodParameter(paramNames[i], paramValues[i]);
                }
            }

            // Add local variables
            if (localNames != null)
            {
                for (int i = 0; ((i < localNames.Length) && (i < localValues.Length)); i++)
                {
                    ms.AddLocalVariable(localNames[i], localValues[i]);
                }
            }

            ms.Lock();

            return(sde);
        }
예제 #5
0
        /// <summary>
        /// Throw an instance of <see cref="SelfDocumentingException"/>.
        /// </summary>
        ///
        /// <param name="msg">
        /// The message for exception.
        /// </param>
        /// <param name="e">
        /// The cause.
        /// </param>
        /// <param name="methodName">
        /// The method name.
        /// </param>
        /// <param name="paramNames">
        /// The parameter names.
        /// </param>
        /// <param name="paramValues">
        /// The parameter values.
        /// </param>
        /// <param name="localNames">
        /// The local variable names.
        /// </param>
        /// <param name="localValues">
        /// The local variable values.
        /// </param>
        /// <param name="instanceNames">
        /// The instance variable names.
        /// </param>
        /// <param name="instanceValues">
        /// The instance variable values.
        /// </param>
        /// <returns>
        /// An instance of <see cref="SelfDocumentingException"/>.
        /// </returns>
        private static SelfDocumentingException PopulateSDE(string msg, Exception e, string methodName,
                                                            string[] paramNames, object[] paramValues, string[] localNames, object[] localValues,
                                                            string[] instanceNames, object[] instanceValues)
        {
            // Create SelfDocumentingException instance if necessary
            SelfDocumentingException sde;

            if (e is SelfDocumentingException)
            {
                sde = (SelfDocumentingException)e;
            }
            else
            {
                // Create SelfDocumentingException
                sde = new SelfDocumentingException(msg, e);
            }

            // pin method
            MethodState ms = sde.PinMethod(methodName, sde.StackTrace);

            // add method parameter
            for (int i = 0; i < paramNames.Length; i++)
            {
                ms.AddMethodParameter(paramNames[i], paramValues[i]);
            }

            // add local variable
            for (int i = 0; i < localNames.Length; i++)
            {
                ms.AddLocalVariable(localNames[i], localValues[i]);
            }

            // add instance variable
            for (int i = 0; i < instanceNames.Length; i++)
            {
                ms.AddInstanceVariable(instanceNames[i], instanceValues[i]);
            }

            // lock exception info
            ms.Lock();

            // return it
            return(sde);
        }