/// <summary>
        /// Obtains a failure from a reason and exception.
        /// </summary>
        /// <param name="reason">  the reason </param>
        /// <param name="cause">  the cause </param>
        /// <returns> the failure </returns>
        public static FailureItem of(FailureReason reason, Exception cause)
        {
            ArgChecker.notNull(reason, "reason");
            ArgChecker.notNull(cause, "cause");
            string causeMessage = cause.Message;
            string message      = Strings.isNullOrEmpty(causeMessage) ? cause.GetType().Name : causeMessage;

            return(FailureItem.of(reason, cause, message));
        }
Exemple #2
0
 /// <summary>
 /// Creates an instance using a supplier.
 /// <para>
 /// If the supplier succeeds normally, the supplied value will be returned.
 /// If the supplier fails, the empty value will be returned along with a failure.
 ///
 /// </para>
 /// </summary>
 /// @param <T> the type of the value </param>
 /// <param name="emptyValue">  the empty value </param>
 /// <param name="supplier">  supplier of the result value </param>
 /// <returns> an instance containing the supplied value, or a failure if an exception is thrown </returns>
 public static ValueWithFailures <T> of <T>(T emptyValue, System.Func <T> supplier)
 {
     try
     {
         return(of(supplier()));
     }
     catch (Exception ex)
     {
         return(ValueWithFailures.of(emptyValue, FailureItem.of(FailureReason.ERROR, ex)));
     }
 }
Exemple #3
0
        //-------------------------------------------------------------------------
        public virtual void test_of_reasonMessageException()
        {
            System.ArgumentException ex = new System.ArgumentException("exmsg");
            FailureItem test            = FailureItem.of(FailureReason.INVALID, ex, "my failure");

            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "my failure");
            assertTrue(test.CauseType.Present);
            assertEquals(test.CauseType.get(), typeof(System.ArgumentException));
            assertTrue(test.StackTrace.Contains(".test_of_reasonMessageException("));
            assertEquals(test.ToString(), "INVALID: my failure: java.lang.IllegalArgumentException: exmsg");
        }
Exemple #4
0
        //-------------------------------------------------------------------------
        public virtual void test_of_reasonMessage()
        {
            FailureItem test = FailureItem.of(FailureReason.INVALID, "my {} {} failure", "big", "bad");

            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "my big bad failure");
            assertFalse(test.CauseType.Present);
            assertFalse(test.StackTrace.Contains(".FailureItem.of("));
            assertFalse(test.StackTrace.Contains(".Failure.of("));
            assertTrue(test.StackTrace.StartsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure", StringComparison.Ordinal));
            assertTrue(test.StackTrace.Contains(".test_of_reasonMessage("));
            assertEquals(test.ToString(), "INVALID: my big bad failure");
        }
Exemple #5
0
        public virtual void test_of_reasonMessageExceptionNestedExceptionWithAttributes()
        {
            System.ArgumentException innerEx = new System.ArgumentException("inner");
            System.ArgumentException ex      = new System.ArgumentException("exmsg", innerEx);
            FailureItem test = FailureItem.of(FailureReason.INVALID, ex, "a {foo} {bar} failure", "big", "bad");

            assertEquals(test.Attributes, ImmutableMap.of("foo", "big", "bar", "bad", FailureItem.EXCEPTION_MESSAGE_ATTRIBUTE, "exmsg"));
            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "a big bad failure");
            assertTrue(test.CauseType.Present);
            assertEquals(test.CauseType.get(), typeof(System.ArgumentException));
            assertTrue(test.StackTrace.Contains(".test_of_reasonMessageExceptionNestedExceptionWithAttributes("));
            assertEquals(test.ToString(), "INVALID: a big bad failure: java.lang.IllegalArgumentException: exmsg");
        }
Exemple #6
0
        public virtual void test_withAttributes()
        {
            FailureItem test = FailureItem.of(FailureReason.INVALID, "my {one} {two} failure", "big", "bad");

            test = test.withAttributes(ImmutableMap.of("foo", "bar", "two", "good"));
            assertEquals(test.Attributes, ImmutableMap.of("one", "big", "two", "good", "foo", "bar"));
            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "my big bad failure");
            assertFalse(test.CauseType.Present);
            assertFalse(test.StackTrace.Contains(".FailureItem.of("));
            assertFalse(test.StackTrace.Contains(".Failure.of("));
            assertTrue(test.StackTrace.StartsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure", StringComparison.Ordinal));
            assertTrue(test.StackTrace.Contains(".test_withAttributes("));
            assertEquals(test.ToString(), "INVALID: my big bad failure");
        }
Exemple #7
0
        private ValueWithFailures <IList <int> > flatMapFunction(IList <string> input)
        {
            IList <int>         integers = new List <int>();
            IList <FailureItem> failures = new List <FailureItem>();

            foreach (string str in input)
            {
                try
                {
                    integers.Add(Convert.ToInt32(str));
                }
                catch (System.FormatException ex)
                {
                    failures.Add(FailureItem.of(FailureReason.INVALID, ex));
                }
            }
            return(ValueWithFailures.of(integers, failures));
        }
Exemple #8
0
        private static ValueWithFailures <double> mockCalc(double value)
        {
            FailureItem failure = FailureItem.of(FailureReason.CALCULATION_FAILED, "Error calculating result for input value {}", value);

            return(ValueWithFailures.of(value, ImmutableList.of(failure)));
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Obtains a failure from a reason and message.
        /// <para>
        /// The message is produced using a template that contains zero to many "{}" placeholders.
        /// Each placeholder is replaced by the next available argument.
        /// If there are too few arguments, then the message will be left with placeholders.
        /// If there are too many arguments, then the excess arguments are appended to the
        /// end of the message. No attempt is made to format the arguments.
        /// See <seealso cref="Messages#format(String, Object...)"/> for more details.
        /// </para>
        /// <para>
        /// An exception will be created internally to obtain a stack trace.
        /// The cause type will not be present in the resulting failure.
        ///
        /// </para>
        /// </summary>
        /// <param name="reason">  the reason </param>
        /// <param name="message">  a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} </param>
        /// <param name="messageArgs">  the arguments for the message </param>
        /// <returns> the failure </returns>
        public static Failure of(FailureReason reason, string message, params object[] messageArgs)
        {
            string msg = Messages.format(message, messageArgs);

            return(Failure.of(FailureItem.of(reason, msg, 1)));
        }
 /// <summary>
 /// Obtains a failure from a reason and exception.
 /// </summary>
 /// <param name="reason">  the reason </param>
 /// <param name="cause">  the cause </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureReason reason, Exception cause)
 {
     return(Failure.of(FailureItem.of(reason, cause)));
 }
 /// <summary>
 /// Obtains a failure from a reason, message and exception.
 /// <para>
 /// The message is produced using a template that contains zero to many "{}" placeholders.
 /// Each placeholder is replaced by the next available argument.
 /// If there are too few arguments, then the message will be left with placeholders.
 /// If there are too many arguments, then the excess arguments are appended to the
 /// end of the message. No attempt is made to format the arguments.
 /// See <seealso cref="Messages#format(String, Object...)"/> for more details.
 ///
 /// </para>
 /// </summary>
 /// <param name="reason">  the reason </param>
 /// <param name="cause">  the cause </param>
 /// <param name="message">  the failure message, possibly containing placeholders, formatted using <seealso cref="Messages#format"/> </param>
 /// <param name="messageArgs">  arguments used to create the failure message </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureReason reason, Exception cause, string message, params object[] messageArgs)
 {
     return(Failure.of(FailureItem.of(reason, cause, message, messageArgs)));
 }