Example #1
0
        public virtual void test_withAttribute()
        {
            FailureItem test = FailureItem.of(FailureReason.INVALID, "my {one} {two} failure", "big", "bad");

            test = test.withAttribute("foo", "bar");
            assertEquals(test.Attributes, ImmutableMap.of("one", "big", "two", "bad", "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_withAttribute("));
            assertEquals(test.ToString(), "INVALID: my big bad failure");
        }
        /// <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);
        }