/** * Obtains a failure from a reason and exception. * * @param reason the reason * @param cause the cause * @return the failure */ public static Failure of(FailureReason reason, Exception cause) { return Failure.of(FailureItem.of(reason, cause)); }
/** * Obtains a failure from a reason, message and exception. * <p> * 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 {@link Messages#format(String, Object...)} for more details. * * @param reason the reason * @param cause the cause * @param message the failure message, possibly containing placeholders, formatted using {@link Messages#format} * @param messageArgs arguments used to create the failure message * @return the failure */ public static Failure of(FailureReason reason, Exception cause, String message, Object... messageArgs) { return Failure.of(FailureItem.of(reason, cause, message, messageArgs)); }
/** * Obtains a failure from a reason and exception. * * @param reason the reason * @param cause the cause * @return the failure */ public static FailureItem of(FailureReason reason, Throwable cause) { ArgChecker.notNull(reason, "reason"); ArgChecker.notNull(cause, "cause"); String causeMessage = cause.getMessage(); String message = Strings.isNullOrEmpty(causeMessage) ? cause.getClass().getSimpleName() : causeMessage; return FailureItem.of(reason, cause, message); }
/** * Creates an instance using a supplier. * <p> * 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. * * @param <T> the type of the value * @param emptyValue the empty value * @param supplier supplier of the result value * @return an instance containing the supplied value, or a failure if an exception is thrown */ public static <T> ValueWithFailures<T> of(T emptyValue, Supplier<T> supplier) { try { return of(supplier.get()); } catch (Exception ex) { return ValueWithFailures.of(emptyValue, FailureItem.of(FailureReason.ERROR, ex)); } }
private <T extends Trade> ValueWithFailures<List<T>> parseFile(CharSource charSource, Class<T> tradeType) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.headers().contains(TYPE_FIELD)) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of(FailureReason.PARSING, "CSV file does not contain '{header}' header: {}", TYPE_FIELD, charSource)); } return parseFile(csv, tradeType); } catch (RuntimeException ex) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of( FailureReason.PARSING, ex, "CSV file could not be parsed: {exceptionMessage}: {}", ex.getMessage(), charSource)); } }
private <T extends Position> ValueWithFailures<List<T>> parseFile(CharSource charSource, Class<T> positionType) { try (CsvIterator csv = CsvIterator.of(charSource, true)) { if (!csv.headers().contains(TYPE_FIELD)) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of(FailureReason.PARSING, "CSV file does not contain '{header}' header: {}", TYPE_FIELD, charSource)); } return parseFile(csv, positionType); } catch (RuntimeException ex) { return ValueWithFailures.of( ImmutableList.of(), FailureItem.of( FailureReason.PARSING, ex, "CSV file could not be parsed: {exceptionMessage}: {}", ex.getMessage(), charSource)); } }
/** * Obtains a failure from a reason and message. * <p> * 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 {@link Messages#format(String, Object...)} for more details. * <p> * An exception will be created internally to obtain a stack trace. * The cause type will not be present in the resulting failure. * * @param reason the reason * @param message a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return the failure */ public static Failure of(FailureReason reason, String message, Object... messageArgs) { String msg = Messages.format(message, messageArgs); return Failure.of(FailureItem.of(reason, msg, 1)); }
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)); }
private ValueWithFailures<List<Integer>> flatMapFunction(List<String> input) { List<Integer> integers = new ArrayList<>(); List<FailureItem> failures = new ArrayList<>(); for (String str : input) { try { integers.add(Integer.valueOf(str)); } catch (NumberFormatException ex) { failures.add(FailureItem.of(FailureReason.INVALID, ex)); } } return ValueWithFailures.of(integers, failures); }
/** * Obtains a failure from a reason and message. * <p> * The message is produced using a template that contains zero to many "{}" or "{abc}" placeholders. * Each placeholder is replaced by the next available argument. * If the placeholder has a name, its value is added to the attributes map with the name as a key. * 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 {@link Messages#formatWithAttributes(String, Object...)} for more details. * <p> * An exception will be created internally to obtain a stack trace. * The cause type will not be present in the resulting failure. * * @param reason the reason * @param message a message explaining the failure, not empty, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return the failure */ public static FailureItem of(FailureReason reason, String message, Object... messageArgs) { Pair<String, Map<String, String>> msg = Messages.formatWithAttributes(message, messageArgs); return of(reason, msg.getFirst(), msg.getSecond()); }
/** * Creates a failed result specifying the failure reason. * <p> * 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 {@link Messages#format(String, Object...)} for more details. * * @param <R> the expected type of the result * @param reason the result reason * @param message a message explaining the failure, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return a failure result */ public static <R> Result<R> failure(FailureReason reason, String message, Object... messageArgs) { String msg = Messages.format(message, messageArgs); return new Result<>(Failure.of(FailureItem.of(reason, msg, 1))); }
public void test_of_reasonMessageExceptionNestedException() { IllegalArgumentException innerEx = new IllegalArgumentException("inner"); IllegalArgumentException ex = new IllegalArgumentException("exmsg", innerEx); FailureItem test = FailureItem.of(FailureReason.INVALID, ex, "my {} {} failure", "big", "bad"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertTrue(test.getCauseType().isPresent()); assertEquals(test.getCauseType().get(), IllegalArgumentException.class); assertTrue(test.getStackTrace().contains(".test_of_reasonMessageExceptionNestedException(")); assertEquals(test.toString(), "INVALID: my big bad failure: java.lang.IllegalArgumentException: exmsg"); }
public void test_of_reasonMessageException() { IllegalArgumentException ex = new IllegalArgumentException("exmsg"); FailureItem test = FailureItem.of(FailureReason.INVALID, ex, "my failure"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my failure"); assertTrue(test.getCauseType().isPresent()); assertEquals(test.getCauseType().get(), IllegalArgumentException.class); assertTrue(test.getStackTrace().contains(".test_of_reasonMessageException(")); assertEquals(test.toString(), "INVALID: my failure: java.lang.IllegalArgumentException: exmsg"); }
public void test_of_reasonMessage() { FailureItem test = FailureItem.of(FailureReason.INVALID, "my {} {} failure", "big", "bad"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertFalse(test.getCauseType().isPresent()); assertFalse(test.getStackTrace().contains(".FailureItem.of(")); assertFalse(test.getStackTrace().contains(".Failure.of(")); assertTrue(test.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(test.getStackTrace().contains(".test_of_reasonMessage(")); assertEquals(test.toString(), "INVALID: my big bad failure"); }
public void test_of_reasonException() { IllegalArgumentException ex = new IllegalArgumentException("exmsg"); FailureItem test = FailureItem.of(FailureReason.INVALID, ex); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "exmsg"); assertTrue(test.getCauseType().isPresent()); assertEquals(test.getCauseType().get(), IllegalArgumentException.class); assertTrue(test.getStackTrace().contains(".test_of_reasonException(")); assertEquals(test.toString(), "INVALID: exmsg: java.lang.IllegalArgumentException"); }
public void test_of_reasonMessageExceptionNestedExceptionWithAttributes() { IllegalArgumentException innerEx = new IllegalArgumentException("inner"); IllegalArgumentException ex = new IllegalArgumentException("exmsg", innerEx); FailureItem test = FailureItem.of(FailureReason.INVALID, ex, "a {foo} {bar} failure", "big", "bad"); assertEquals(test.getAttributes(), ImmutableMap.of("foo", "big", "bar", "bad", FailureItem.EXCEPTION_MESSAGE_ATTRIBUTE, "exmsg")); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "a big bad failure"); assertTrue(test.getCauseType().isPresent()); assertEquals(test.getCauseType().get(), IllegalArgumentException.class); assertTrue(test.getStackTrace().contains(".test_of_reasonMessageExceptionNestedExceptionWithAttributes(")); assertEquals(test.toString(), "INVALID: a big bad failure: java.lang.IllegalArgumentException: exmsg"); }
public void test_of_reasonError() { NoClassDefFoundError ex = new NoClassDefFoundError("exmsg"); FailureItem test = FailureItem.of(FailureReason.INVALID, ex); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "exmsg"); assertEquals(test.getCauseType().isPresent(), true); assertEquals(test.getCauseType().get(), NoClassDefFoundError.class); assertEquals(test.getStackTrace().contains(".test_of_reasonError("), true); assertEquals(test.toString(), "INVALID: exmsg: java.lang.NoClassDefFoundError"); }
public void test_of_reasonMessageWithAttributes() { IllegalArgumentException innerEx = new IllegalArgumentException("inner"); IllegalArgumentException ex = new IllegalArgumentException("exmsg", innerEx); FailureItem test = FailureItem.of(FailureReason.INVALID, ex, "failure: {exceptionMessage}", "error"); assertEquals(test.getAttributes(), ImmutableMap.of(FailureItem.EXCEPTION_MESSAGE_ATTRIBUTE, "error")); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "failure: error"); assertTrue(test.getCauseType().isPresent()); assertEquals(test.getCauseType().get(), IllegalArgumentException.class); assertTrue(test.getStackTrace().contains(".test_of_reasonMessageWithAttributes(")); assertEquals(test.toString(), "INVALID: failure: error: java.lang.IllegalArgumentException: exmsg"); }
public 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.getAttributes(), ImmutableMap.of("one", "big", "two", "good", "foo", "bar")); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertFalse(test.getCauseType().isPresent()); assertFalse(test.getStackTrace().contains(".FailureItem.of(")); assertFalse(test.getStackTrace().contains(".Failure.of(")); assertTrue(test.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(test.getStackTrace().contains(".test_withAttributes(")); assertEquals(test.toString(), "INVALID: my big bad failure"); }
public void test_withAttribute() { FailureItem test = FailureItem.of(FailureReason.INVALID, "my {one} {two} failure", "big", "bad"); test = test.withAttribute("foo", "bar"); assertEquals(test.getAttributes(), ImmutableMap.of("one", "big", "two", "bad", "foo", "bar")); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertFalse(test.getCauseType().isPresent()); assertFalse(test.getStackTrace().contains(".FailureItem.of(")); assertFalse(test.getStackTrace().contains(".Failure.of(")); assertTrue(test.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(test.getStackTrace().contains(".test_withAttribute(")); assertEquals(test.toString(), "INVALID: my big bad failure"); }