diff --git a/CHANGELOG b/CHANGELOG index aaf77f9..4f79371 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ CHANGELOG ========= +1.2.0 +----- + +* [result] orElseThrow() throws error within a CheckedErrorResultException + 1.1.0 ----- diff --git a/README.org b/README.org index 266002d..89230a7 100644 --- a/README.org +++ b/README.org @@ -821,7 +821,7 @@ **** =T orElseThrow()= Extracts the successful value from the result, or throws the error - =Throwable=. + within a =CheckedErrorResultException=. #+BEGIN_SRC java final Integer result = Result.of(() -> getValue()) diff --git a/src/main/java/net/kemitix/mon/result/CheckedErrorResultException.java b/src/main/java/net/kemitix/mon/result/CheckedErrorResultException.java new file mode 100644 index 0000000..47a1916 --- /dev/null +++ b/src/main/java/net/kemitix/mon/result/CheckedErrorResultException.java @@ -0,0 +1,46 @@ +/** + * 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; + +/** + * A checked wrapper for exceptions thrown within a {@link Result}. + * + *

Used by {@link Result#orElseThrow()} when the {@link Result} is an error.

+ * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public final class CheckedErrorResultException extends Exception { + + private CheckedErrorResultException(final Throwable cause) { + super(cause); + } + + /** + * Creates a new object. + * + * @param cause the cause + * @return a {@link CheckedErrorResultException} containing the cause + */ + static CheckedErrorResultException with(final Throwable cause) { + return new CheckedErrorResultException(cause); + } +} diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index 01bf9b5..e2c0a67 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -73,8 +73,8 @@ class Err implements Result { } @Override - public T orElseThrow() throws Throwable { - throw error; + public T orElseThrow() throws CheckedErrorResultException { + throw CheckedErrorResultException.with(error); } @Override diff --git a/src/main/java/net/kemitix/mon/result/ErrorResultException.java b/src/main/java/net/kemitix/mon/result/ErrorResultException.java index 47e3a90..3ad4893 100644 --- a/src/main/java/net/kemitix/mon/result/ErrorResultException.java +++ b/src/main/java/net/kemitix/mon/result/ErrorResultException.java @@ -22,7 +22,7 @@ package net.kemitix.mon.result; /** - * An unchecked wrapper fot exceptions thrown within a {@link Result}. + * An unchecked wrapper for exceptions thrown within a {@link Result}. * *

Used by {@link Result#orElseThrowUnchecked()} when the {@link Result} is an error.

* diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 35b29af..76d852b 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -96,23 +96,21 @@ public interface Result extends Functor> { * @param the type of the Maybe and the Result * @return a Result containing the value of the Maybe when it is a Just, or the error when it is Nothing */ - @SuppressWarnings("illegalcatch") public static Maybe toMaybe(final Result result) { try { return Maybe.just(result.orElseThrow()); - } catch (final Throwable throwable) { + } catch (final CheckedErrorResultException throwable) { return Maybe.nothing(); } } /** - * Extracts the successful value from the result, or throws the error Throwable. + * Extracts the successful value from the result, or throws the error within a {@link CheckedErrorResultException}. * * @return the value if a success - * @throws Throwable if the result is an error + * @throws CheckedErrorResultException if the result is an error */ - @SuppressWarnings("illegalthrows") - public abstract T orElseThrow() throws Throwable; + public abstract T orElseThrow() throws CheckedErrorResultException; /** * Extracts the successful value from the result, or throws the error Throwable. diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index a8cc577..2754456 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -2,6 +2,7 @@ package net.kemitix.mon; import lombok.RequiredArgsConstructor; import net.kemitix.mon.maybe.Maybe; +import net.kemitix.mon.result.CheckedErrorResultException; import net.kemitix.mon.result.ErrorResultException; import net.kemitix.mon.result.UnexpectedErrorResultException; import net.kemitix.mon.result.Result; @@ -294,7 +295,9 @@ public class ResultTest implements WithAssertions { final RuntimeException exception = new RuntimeException(); final Result error = Result.error(exception); //when - assertThatThrownBy(() -> error.orElseThrow()).isSameAs(exception); + assertThatThrownBy(() -> error.orElseThrow()) + .isInstanceOf(CheckedErrorResultException.class) + .hasCause(exception); } @Test public void okay_whenOrElseThrowT_isValue() throws Exception {