[result] orElseThrow() wraps the error exception in an unchecked MonResultException

This commit is contained in:
Paul Campbell 2018-10-03 21:58:33 +01:00
parent 9a7d641c4f
commit c0ee1cdbc7
6 changed files with 57 additions and 7 deletions

View file

@ -1,6 +1,11 @@
CHANGELOG
=========
1.1.0
-----
* [result] orElseThrow() wraps the error exception in an unchecked MonResultException
1.0.0
-----

View file

@ -806,6 +806,7 @@
.thenWith(v -> () -> {throw new IOException();});
#+END_SRC
**** =Result<Maybe<T>> maybe(Predicate<T> predicate)=
Wraps the value within the Result in a Maybe, either a Just if the
@ -820,7 +821,7 @@
**** =T orElseThrow()=
Extracts the successful value from the result, or throws the error
Throwable.
as the cause within a the unchecked exception =MonResultException=.
#+BEGIN_SRC java
final Integer result = Result.of(() -> getValue())
@ -972,6 +973,7 @@
final Lazy<String> stringLazy = uuidLazy.map(v -> v.toString());
#+END_SRC
** Either
Allows handling a value that can be one of two types, a left value/type or a

View file

@ -73,8 +73,8 @@ class Err<T> implements Result<T> {
}
@Override
public T orElseThrow() throws Throwable {
throw error;
public T orElseThrow() throws MonResultException {
throw MonResultException.with(error);
}
@Override

View file

@ -0,0 +1,44 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.mon.result;
/**
* An Unchecked wrapper for exceptions thrown within a {@code Result}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public final class MonResultException extends RuntimeException {
private MonResultException(final Throwable cause) {
super(cause);
}
/**
* Creates a new object.
*
* @param cause the cause
* @return a MonResultException contain the cause
*/
static MonResultException with(final Throwable cause) {
return new MonResultException(cause);
}
}

View file

@ -109,10 +109,9 @@ public interface Result<T> extends Functor<T, Result<?>> {
* Extracts the successful value from the result, or throws the error Throwable.
*
* @return the value if a success
* @throws Throwable the result is an error
* @throws MonResultException if the result is an error
*/
@SuppressWarnings("illegalthrows")
public abstract T orElseThrow() throws Throwable;
public abstract T orElseThrow() throws MonResultException;
/**
* Swaps the inner Result of a Maybe, so that a Result is on the outside.

View file

@ -292,7 +292,7 @@ public class ResultTest implements WithAssertions {
final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception);
//when
assertThatThrownBy(() -> error.orElseThrow()).isSameAs(exception);
assertThatThrownBy(() -> error.orElseThrow()).hasCause(exception);
}
@Test