/// <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));
        }
        //-------------------------------------------------------------------------
        public virtual void failureDeduplicateFailure()
        {
            Result <object> result  = Result.failure(MISSING_DATA, "failure");
            FailureItem     failure = result.Failure.Items.GetEnumerator().next();

            Result <object> test = Result.failure(result, result);

            assertEquals(test.Failure.Items.size(), 1);
            assertEquals(test.Failure.Items, ImmutableSet.of(failure));
            assertEquals(test.Failure.Message, "failure");
        }
Esempio n. 3
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)));
     }
 }
Esempio n. 4
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");
        }
 //-----------------------------------------------------------------------
 public override bool Equals(object obj)
 {
     if (obj == this)
     {
         return(true);
     }
     if (obj != null && obj.GetType() == this.GetType())
     {
         FailureItem other = (FailureItem)obj;
         return(JodaBeanUtils.equal(reason, other.reason) && JodaBeanUtils.equal(message, other.message) && JodaBeanUtils.equal(attributes, other.attributes) && JodaBeanUtils.equal(stackTrace, other.stackTrace) && JodaBeanUtils.equal(causeType, other.causeType));
     }
     return(false);
 }
Esempio n. 6
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");
        }
        public virtual void ofNullable_null()
        {
            Result <int> test = Result.ofNullable(null);

            assertTrue(test.Failure);
            assertEquals(test.Failure.Message, "Found null where a value was expected");
            assertEquals(test.Failure.Items.size(), 1);
            FailureItem item = test.Failure.Items.GetEnumerator().next();

            assertEquals(item.Reason, MISSING_DATA);
            assertEquals(item.Message, "Found null where a value was expected");
            assertEquals(item.CauseType.Present, false);
            assertTrue(!string.ReferenceEquals(item.StackTrace, null));
        }
Esempio n. 8
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");
        }
        //-------------------------------------------------------------------------
        public virtual void failure_fromFailure()
        {
            Failure      failure = Failure.of(ERROR, "my failure");
            Result <int> test    = Result.failure(failure);

            assertTrue(test.Failure);
            assertEquals(test.Failure.Message, "my failure");
            assertEquals(test.Failure.Items.size(), 1);
            FailureItem item = test.Failure.Items.GetEnumerator().next();

            assertEquals(item.Reason, ERROR);
            assertEquals(item.Message, "my failure");
            assertEquals(item.CauseType.Present, false);
            assertTrue(!string.ReferenceEquals(item.StackTrace, null));
        }
        /// <summary>
        /// Obtains a failure from a reason, exception 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#formatWithAttributes(String, Object...)"/> for more details.
        ///
        /// </para>
        /// </summary>
        /// <param name="reason">  the reason </param>
        /// <param name="cause">  the cause </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 FailureItem of(FailureReason reason, Exception cause, string message, params object[] messageArgs)
        {
            ArgChecker.notNull(reason, "reason");
            ArgChecker.notNull(cause, "cause");
            Pair <string, IDictionary <string, string> > msg = Messages.formatWithAttributes(message, messageArgs);
            string      stackTrace   = Throwables.getStackTraceAsString(cause).replace(Environment.NewLine, "\n");
            FailureItem @base        = new FailureItem(reason, msg.First, msg.Second, stackTrace, cause.GetType());
            string      causeMessage = cause.Message;

            if ([email protected](EXCEPTION_MESSAGE_ATTRIBUTE) && !Strings.isNullOrEmpty(causeMessage))
            {
                return(@base.withAttribute(EXCEPTION_MESSAGE_ATTRIBUTE, causeMessage));
            }
            return(@base);
        }
Esempio n. 11
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");
        }
        //-------------------------------------------------------------------------
        public virtual void generatedStackTrace_Failure()
        {
            Failure test = Failure.of(FailureReason.INVALID, "my {} {} failure", "big", "bad");

            assertEquals(test.Reason, FailureReason.INVALID);
            assertEquals(test.Message, "my big bad failure");
            assertEquals(test.Items.size(), 1);
            FailureItem item = test.Items.GetEnumerator().next();

            assertFalse(item.CauseType.Present);
            assertFalse(item.StackTrace.Contains(".FailureItem.of("));
            assertFalse(item.StackTrace.Contains(".Failure.of("));
            assertTrue(item.StackTrace.StartsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure", StringComparison.Ordinal));
            assertTrue(item.StackTrace.Contains(".generatedStackTrace_Failure("));
            assertEquals(item.ToString(), "INVALID: my big bad failure");
        }
        //-------------------------------------------------------------------------
        public virtual void failure_fromResult_failure()
        {
            Result <string> failure = Result.failure(ERROR, "my failure");
            Result <int>    test    = Result.failure(failure);

            assertTrue(test.Failure);
            assertEquals(test.Failure.Message, "my failure");
            assertEquals(test.Failure.Items.size(), 1);
            FailureItem item = test.Failure.Items.GetEnumerator().next();

            assertEquals(item.Reason, ERROR);
            assertEquals(item.Message, "my failure");
            assertEquals(item.CauseType.Present, false);
            assertEquals(item.StackTrace.Contains(".FailureItem.of("), false);
            assertEquals(item.StackTrace.Contains(".Failure.of("), false);
            assertEquals(item.StackTrace.Contains(".Result.failure("), false);
        }
Esempio n. 14
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));
        }
        //-------------------------------------------------------------------------
        public virtual void failure()
        {
            System.ArgumentException ex   = new System.ArgumentException("failure");
            Result <string>          test = Result.failure(ex);

            assertEquals(test.Success, false);
            assertEquals(test.Failure, true);
            assertEquals(test.getValueOrElse("blue"), "blue");
            assertEquals(test.getValueOrElseApply(f => "blue"), "blue");
//JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter:
            assertEquals(test.getValueOrElseApply(Failure::getMessage), "failure");
            assertThrowsIllegalArg(() => test.getValueOrElse(null));
            assertThrowsIllegalArg(() => test.getValueOrElseApply(null));
            assertEquals(test.Failure.Reason, ERROR);
            assertEquals(test.Failure.Message, "failure");
            assertEquals(test.Failure.Items.size(), 1);
            FailureItem item = test.Failure.Items.GetEnumerator().next();

            assertEquals(item.Reason, ERROR);
            assertEquals(item.Message, "failure");
            assertEquals(item.CauseType.get(), ex.GetType());
            assertEquals(item.StackTrace, Throwables.getStackTraceAsString(ex).replace(Environment.NewLine, "\n"));
        }
Esempio n. 16
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)));
        }
Esempio n. 17
0
 //-------------------------------------------------------------------------
 /// <summary>
 /// Adds a failure to the list.
 /// </summary>
 /// <param name="failure">  the failure to add </param>
 /// <returns> this, for chaining </returns>
 public FailureItemsBuilder addFailure(FailureItem failure)
 {
     listBuilder.add(failure);
     return(this);
 }
Esempio n. 18
0
 /// <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)));
 }
Esempio n. 19
0
 /// <summary>
 /// Obtains a failure for a single failure item.
 /// </summary>
 /// <param name="item">  the failure item </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureItem item)
 {
     return(new Failure(item.Reason, item.Message, ImmutableSet.of(item)));
 }
Esempio n. 20
0
        //-------------------------------------------------------------------------
        public virtual void test_of_reasonMessageShortStackTrace()
        {
            FailureItem test = FailureItem.meta().builder().set("reason", FailureReason.INVALID).set("message", "my issue").set("stackTrace", "Short stack trace").set("causeType", typeof(System.ArgumentException)).build();

            assertEquals(test.ToString(), "INVALID: my issue: Short stack trace");
        }
Esempio n. 21
0
 /// <summary>
 /// Obtains a failure for multiple failure items.
 /// </summary>
 /// <param name="item">  the first failure item </param>
 /// <param name="additionalItems">  additional failure items </param>
 /// <returns> the failure </returns>
 public static Failure of(FailureItem item, params FailureItem[] additionalItems)
 {
     return(of(ImmutableSet.builder <FailureItem>().add(item).add(additionalItems).build()));
 }
Esempio n. 22
0
        //-------------------------------------------------------------------------
        /// <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)));
        }
Esempio n. 23
0
 /// <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)));
 }