/** * 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)); }
@Override public FailureItem build() { return new FailureItem( reason, message, attributes, stackTrace, causeType); }
public void failure_fromStatusMessageArgs_placeholdersLessThanArgs1() { Result<String> failure = Result.failure(ERROR, "my {} failure", "blue", "rabbit"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my blue failure - [rabbit]"); }
public void failureDifferentTypes() { Result<Object> failure1 = Result.failure(MISSING_DATA, "message 1"); Result<Object> failure2 = Result.failure(CALCULATION_FAILED, "message 2"); Result<Object> failure3 = Result.failure(ERROR, "message 3"); Result<?> composite = Result.failure(failure1, failure2, failure3); assertEquals(composite.getFailure().getReason(), FailureReason.MULTIPLE); assertEquals(composite.getFailure().getMessage(), "message 1, message 2, message 3"); }
public void test_constructor_failure() { Failure failure = Failure.of(FailureReason.UNSUPPORTED, "Test"); FailureException test = new FailureException(failure); assertEquals(test.getFailure(), failure); }
/** * Returns a new instance with the specified value, retaining the current failures. * <p> * This can be useful as an inline alternative to {@link #map(Function)}. * * @param <R> the type of the value in the returned result * @param value the new value * @return the combined instance of value and failures */ public <R> ValueWithFailures<R> withValue(R value) { return ValueWithFailures.of(value, this.failures); }
@Override public Result<T> build() { return new Result<>( value, failure); }
/** * Creates an instance wrapping the success value and failures. * * @param <T> the type of the success value * @param successValue the success value * @param failures the failures * @return an instance wrapping the value and failures */ public static <T> ValueWithFailures<T> of(T successValue, List<FailureItem> failures) { return new ValueWithFailures<>(successValue, failures); }
private Result( T value, Failure failure) { this.value = value; this.failure = failure; validate(); }
@Override public FailureItems build() { return new FailureItems( failures); }
/** * Creates a builder to create the list of failures. * * @return a builder */ public static FailureItemsBuilder builder() { return new FailureItemsBuilder(); }
@Override public Failure build() { return new Failure( reason, message, items); }
public void failure_fromStatusMessageArgs_placeholdersMatchArgs1() { Result<String> failure = Result.failure(ERROR, "my {} failure", "blue"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my blue failure"); }
/** * 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)); }
/** * Creates a failed result containing a failure. * <p> * This is useful for converting an existing {@code Failure} instance to a result. * * @param <R> the expected type of the result * @param failure details of the failure * @return a failed result containing the specified failure */ public static <R> Result<R> failure(Failure failure) { return new Result<>(failure); }
@Override public ValueWithFailures<T> build() { return new ValueWithFailures<>( value, failures); }
public void failure_fromStatusMessageArgs_placeholdersLessThanArgs3() { Result<String> failure = Result.failure(ERROR, "my failure", "blue", "rabbit", "carrot"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my failure - [blue, rabbit, carrot]"); }
public void failure_fromStatusMessageArgs_placeholdersExceedArgs() { Result<String> failure = Result.failure(ERROR, "my {} {} failure", "blue"); Result<Integer> test = Result.failure(failure); assertTrue(test.isFailure()); assertEquals(test.getFailure().getMessage(), "my blue {} failure"); }