From c0ee1cdbc72d85d2832146c01d268e1f7a38e4a9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 3 Oct 2018 21:58:33 +0100 Subject: [PATCH] [result] orElseThrow() wraps the error exception in an unchecked MonResultException --- CHANGELOG | 5 +++ README.org | 4 +- src/main/java/net/kemitix/mon/result/Err.java | 4 +- .../mon/result/MonResultException.java | 44 +++++++++++++++++++ .../java/net/kemitix/mon/result/Result.java | 5 +-- src/test/java/net/kemitix/mon/ResultTest.java | 2 +- 6 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/kemitix/mon/result/MonResultException.java diff --git a/CHANGELOG b/CHANGELOG index f0f991d..1462f1c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ CHANGELOG ========= +1.1.0 +----- + +* [result] orElseThrow() wraps the error exception in an unchecked MonResultException + 1.0.0 ----- diff --git a/README.org b/README.org index 1550087..0faeaa0 100644 --- a/README.org +++ b/README.org @@ -806,6 +806,7 @@ .thenWith(v -> () -> {throw new IOException();}); #+END_SRC + **** =Result> maybe(Predicate 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 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 diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index e2199ad..b22de8a 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 MonResultException { + throw MonResultException.with(error); } @Override diff --git a/src/main/java/net/kemitix/mon/result/MonResultException.java b/src/main/java/net/kemitix/mon/result/MonResultException.java new file mode 100644 index 0000000..6611ebd --- /dev/null +++ b/src/main/java/net/kemitix/mon/result/MonResultException.java @@ -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); + } +} diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 86d4819..fcb1022 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -109,10 +109,9 @@ public interface Result extends Functor> { * 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. diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 3805a24..e15109f 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -292,7 +292,7 @@ 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()).hasCause(exception); } @Test