/** * Obtains a failure for multiple failure items. * * @param item the first failure item * @param additionalItems additional failure items * @return the failure */ public static Failure of(FailureItem item, FailureItem... additionalItems) { return of(ImmutableSet.<FailureItem>builder().add(item).add(additionalItems).build()); }
/** * 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)); }
/** * Creates a failed result caused by an exception. * <p> * The failure will have a reason of {@code ERROR}. * * @param <R> the expected type of the result * @param exception the cause of the failure * @return a failure result */ public static <R> Result<R> failure(Exception exception) { return new Result<>(Failure.of(FailureReason.ERROR, exception)); }
/** * Creates a failed result caused by an exception with a specified reason. * * @param <R> the expected type of the result * @param reason the result reason * @param exception the cause of the failure * @return a failure result */ public static <R> Result<R> failure(FailureReason reason, Exception exception) { return new Result<>(Failure.of(reason, exception)); }
/** * 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)); }
/** * Creates a failed result caused by an exception. * <p> * The failure will have a reason of {@code ERROR}. * <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 exception the cause of the failure * @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(Exception exception, String message, Object... messageArgs) { return new Result<>(Failure.of(FailureReason.ERROR, exception, message, messageArgs)); }
/** * Creates a failed result caused by an exception with a specified 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. * * @param <R> the expected type of the result * @param reason the result reason * @param exception the cause of the failure * @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, Exception exception, String message, Object... messageArgs) { return new Result<>(Failure.of(reason, exception, message, messageArgs)); }
/** * 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)); }
/** * 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))); }
/** * Creates a failed result combining multiple failed results. * <p> * The input results can be successes or failures, only the failures will be included in the created result. * Intended to be used with {@link #anyFailures(Iterable)}. * <blockquote><pre> * if (Result.anyFailures(results) { * return Result.failure(results); * } * </pre></blockquote> * * @param <R> the expected type of the result * @param results multiple results, of which at least one must be a failure, not empty * @return a failed result wrapping multiple other failed results * @throws IllegalArgumentException if results is empty or contains nothing but successes */ public static <R> Result<R> failure(Iterable<? extends Result<?>> results) { ArgChecker.notEmpty(results, "results"); ImmutableSet<FailureItem> items = Guavate.stream(results) .filter(Result::isFailure) .map(Result::getFailure) .flatMap(f -> f.getItems().stream()) .collect(Guavate.toImmutableSet()); if (items.isEmpty()) { throw new IllegalArgumentException("All results were successes"); } return new Result<>(Failure.of(items)); }
public void test_constructor_failure() { Failure failure = Failure.of(FailureReason.UNSUPPORTED, "Test"); FailureException test = new FailureException(failure); assertEquals(test.getFailure(), failure); }
public void generatedStackTrace_Failure() { Failure test = Failure.of(FailureReason.INVALID, "my {} {} failure", "big", "bad"); assertEquals(test.getReason(), FailureReason.INVALID); assertEquals(test.getMessage(), "my big bad failure"); assertEquals(test.getItems().size(), 1); FailureItem item = test.getItems().iterator().next(); assertFalse(item.getCauseType().isPresent()); assertFalse(item.getStackTrace().contains(".FailureItem.of(")); assertFalse(item.getStackTrace().contains(".Failure.of(")); assertTrue(item.getStackTrace().startsWith("com.opengamma.strata.collect.result.FailureItem: my big bad failure")); assertTrue(item.getStackTrace().contains(".generatedStackTrace_Failure(")); assertEquals(item.toString(), "INVALID: my big bad failure"); }
public void failure_fromFailure() { Failure failure = Failure.of(ERROR, "my failure"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my failure"); assertEquals(test.getFailure().getItems().size(), 1); FailureItem item = test.getFailure().getItems().iterator().next(); assertEquals(item.getReason(), ERROR); assertEquals(item.getMessage(), "my failure"); assertEquals(item.getCauseType().isPresent(), false); assertTrue(item.getStackTrace() != null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void createByBuilder_bothValueAndFailure() { Result.meta().builder() .set("value", "A") .set("failure", Failure.of(CALCULATION_FAILED, "Fail")) .build(); }