void Contract_ContractFailed(object sender, ContractFailedEventArgs e)
 {
     e.SetHandled();
     throw new AssertException(
         String.Format("{0}: {1}, {2}", e.FailureKind, e.Condition, e.Message)
         );
 }
Пример #2
0
        public static void ContractFailed(object sender, ContractFailedEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            e.SetUnwind();  // Causes the code to throw a ContractException after the event finishes
            Assert.Fail(string.Format(CultureInfo.InvariantCulture, "{0}: {1}\r\nCondition: {2}", e.FailureKind, e.Message, e.Condition));
        }
Пример #3
0
 private static void AssertCore(bool condition, string message)
 {
     if (!condition)
     {
         EventHandler <ContractFailedEventArgs> handler = Interlocked.CompareExchange <EventHandler <ContractFailedEventArgs> >(ref _contractFailed, null, null);
         if (handler != null)
         {
             ContractFailedEventArgs e = new ContractFailedEventArgs();
             handler(null, e);
             if (e.IsUnwined)
             {
                 throw new Exception(message);
             }
         }
         Debug.Assert(condition, message);
     }
 }
 private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
 {
     string str2;
     if ((failureKind < ContractFailureKind.Precondition) || (failureKind > ContractFailureKind.Assume))
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[] { failureKind }), "failureKind");
     }
     string message = "contract failed.";
     ContractFailedEventArgs e = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         message = GetDisplayMessage(failureKind, userMessage, conditionText);
         if (contractFailedEvent != null)
         {
             e = new ContractFailedEventArgs(failureKind, message, conditionText, innerException);
             foreach (EventHandler<ContractFailedEventArgs> handler in contractFailedEvent.GetInvocationList())
             {
                 try
                 {
                     handler(null, e);
                 }
                 catch (Exception exception)
                 {
                     e.thrownDuringHandler = exception;
                     e.SetUnwind();
                 }
             }
             if (e.Unwind)
             {
                 if (Environment.IsCLRHosted)
                 {
                     TriggerCodeContractEscalationPolicy(failureKind, message, conditionText, innerException);
                 }
                 if (innerException == null)
                 {
                     innerException = e.thrownDuringHandler;
                 }
                 throw new ContractException(failureKind, message, userMessage, conditionText, innerException);
             }
         }
     }
     finally
     {
         if ((e != null) && e.Handled)
         {
             str2 = null;
         }
         else
         {
             str2 = message;
         }
     }
     resultFailureMessage = str2;
 }
Пример #5
0
 static void HandleFailure(object sender, ContractFailedEventArgs args)
 {
     Console.WriteLine("{0}: {1} {2}", args.FailureKind,
         args.Condition, args.Message);
     args.SetHandled();
 }
Пример #6
0
 static void Contract_ContractFailed( object sender, ContractFailedEventArgs e )
 {
     Console.WriteLine( "No se pudo asumir algo: " + e.Condition );
     e.SetHandled( );
     Console.ReadKey( );
 }
Пример #7
0
        //public static void ExportAppointmentNotification(AllClientsContactExport export, List<AllClientsWebform> webForms)
        //{
        //    try
        //    {
        //        var autoNotificationForm = webForms.FirstOrDefault(x => x.WebformType == Enumerations.WebformType.AutoNotification);
        //        if (autoNotificationForm == null)
        //            return;
        //        var appointmentReminderForm = webForms.FirstOrDefault(x => x.WebformType == Enumerations.WebformType.AppointmentReminder);
        //        if (appointmentReminderForm == null)
        //            return;
        //        //criteria for auo notification custom variables contains ApptDate
        //        var appointmentDate = export.Contact.Custom.FirstOrDefault(x => x.Name == "ApptDate");
        //        if (appointmentDate == null)
        //            return;
        //        //is the appointment date tommorrow?
        //        var testDate = System.DateTime.MinValue;
        //        if (!DateTime.TryParse(appointmentDate.Value, out testDate))
        //            return;
        //        AllClientsWebform notificationWebform = null;
        //        //appt tomorrow send  the email to client
        //        if (System.DateTime.Now.Date.AddDays(1).Date == testDate.Date)
        //            notificationWebform = appointmentReminderForm;
        //        else if (System.DateTime.Now.Date > testDate.Date && testDate > System.DateTime.Now.AddDays(-2))
        //            notificationWebform = autoNotificationForm;

        //        if (notificationWebform != null)
        //        {
        //            //we will clone a new contact export and send through traditional means
        //            var notifyExport = new AllClientsContactExport
        //            {
        //                Account = export.Account,
        //                AllClientsWebform = notificationWebform,
        //                Contact = export.Contact
        //            };

        //            notifyExport.Contact.Custom.Add(new CustomElement { Name = "Comments", Value = GetAppointmentNotificationComments(notifyExport.Contact) });
        //            ExportContact(notifyExport);
        //        }
        //    }
        //    catch (System.Exception ex)
        //    {
        //        throw new AIMException("Could not create Auto Notification", ex);
        //    }


        //}
        static void Contract_ContractFailed(object sender, ContractFailedEventArgs e)
        {
            throw new AIMException(e.Message);
        }
Пример #8
0
		public static string RaiseContractFailedEvent (ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
		{

			StringBuilder msg = new StringBuilder (60);
			switch (failureKind) {
			case ContractFailureKind.Assert:
				msg.Append ("Assertion failed");
				break;
			case ContractFailureKind.Assume:
				msg.Append ("Assumption failed");
				break;
			case ContractFailureKind.Invariant:
				msg.Append ("Invariant failed");
				break;
			case ContractFailureKind.Postcondition:
				msg.Append ("Postcondition failed");
				break;
			case ContractFailureKind.PostconditionOnException:
				msg.Append ("Postcondition failed after throwing an exception");
				break;
			case ContractFailureKind.Precondition:
				msg.Append ("Precondition failed");
				break;
			default:
				throw new NotSupportedException ("Not supported: " + failureKind);
			}
			if (conditionText != null) {
				msg.Append (": ");
				msg.Append (conditionText);
			} else {
				msg.Append ('.');
			}
			if (userMessage != null) {
				msg.Append ("  ");
				msg.Append (userMessage);
			}
			string msgString = msg.ToString ();

			Exception handlerException = null;
			bool unwind = false, handled = false;

			var contractFailed = Contract.InternalContractFailedEvent;
			if (contractFailed != null) {
				// Execute all event handlers
				var handlers = contractFailed.GetInvocationList ();
				var e = new ContractFailedEventArgs (failureKind, msgString, conditionText, innerException);
				foreach (var handler in handlers) {
					try {
						handler.DynamicInvoke (null, e);
					} catch (Exception ex) {
						e.SetUnwind ();
						// If multiple handlers throw an exception then the specification states that it
						// is undetermined which one becomes the InnerException.
						handlerException = ex.InnerException;
					}
				}
				unwind = e.Unwind;
				handled = e.Handled;
			}

			if (unwind) {
				Exception ex = innerException ?? handlerException;
				throw new ContractException (msgString, failureKind, conditionText, userMessage, ex);
			}

			return handled ? null : msgString;
		}
        static partial void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, String userMessage, String conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", failureKind), "failureKind");
            Contract.EndContractBlock();

            string returnValue;
            String displayMessage = "contract failed.";  // Incomplete, but in case of OOM during resource lookup...
#if !FEATURE_CORECLR || FEATURE_NETCORE
            ContractFailedEventArgs eventArgs = null;  // In case of OOM.
#endif // !FEATURE_CORECLR || FEATURE_NETCORE
#if FEATURE_RELIABILITY_CONTRACTS
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
#if !FEATURE_CORECLR || FEATURE_NETCORE
                EventHandler<ContractFailedEventArgs> contractFailedEventLocal = contractFailedEvent;
                if (contractFailedEventLocal != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler<ContractFailedEventArgs> handler in contractFailedEventLocal.GetInvocationList())
                    {
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        {
                            eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
#if !FEATURE_CORECLR 
                        if (Environment.IsCLRHosted)
                            TriggerCodeContractEscalationPolicy(failureKind, displayMessage, conditionText, innerException);
#endif
                        // unwind
                        if (innerException == null) { innerException = eventArgs.thrownDuringHandler; }
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
#endif // !FEATURE_CORECLR || FEATURE_NETCORE
            }
            finally
            {
#if !FEATURE_CORECLR || FEATURE_NETCORE
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
#endif // !FEATURE_CORECLR || FEATURE_NETCORE
                {
                    returnValue = displayMessage;
                }
            }
            resultFailureMessage = returnValue;
        }
Пример #10
0
        static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
                throw new ArgumentException(string.Format("Invalid enum value: {0}", failureKind), "failureKind");
            Contract.EndContractBlock();

            string returnValue;
            string displayMessage = "contract failed.";  // Incomplete, but in case of OOM during resource lookup...
            ContractFailedEventArgs eventArgs = null;  // In case of OOM.
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler<ContractFailedEventArgs> contractFailedEventLocal = contractFailedEvent;
                if (contractFailedEventLocal != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler<ContractFailedEventArgs> handler in contractFailedEventLocal.GetInvocationList())
                    {
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception /*e*/)
                        {
                            // eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
                        // unwind
                        // if (innerException == null)
                        // {
                        //     innerException = eventArgs.thrownDuringHandler;
                        // }
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
                {
                    returnValue = displayMessage;
                }
            }
            resultFailureMessage = returnValue;
        }
Пример #11
0
 private static void ContractFailureHandler(Object obj, ContractFailedEventArgs args)
 {
     throw new ContractFailedException();
 }
Пример #12
0
 static void Contract_ContractFailed(object sender, System.Diagnostics.Contracts.ContractFailedEventArgs e)
 {
     e.SetHandled();
     Assert.Fail("{0}: {1} {2}", e.FailureKind, e.Message, e.Condition);
 }